Rails在belongs_to中使用了错误的类

时间:2010-05-28 10:46:21

标签: ruby-on-rails associations

我有一个管理软件测试的应用程序和一个名为TestResult的类:

class TestResult < ActiveRecord::Base
  belongs_to :test_case,   :class_name => "TestCase"
end

我现在正在从Rails 1.x迁移到2.3.5。

在Rails 1.x中一切正常。

尝试访问Rails 2.3.5中的关联时,出现以下错误:

NoMethodError:ActiveRecord :: TestCase的未定义方法'find':Class 来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/belongs_to_association.rb:49:in'send'

来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/belongs_to_association.rb:49:in'find_target'

来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:239:in'load_target'

来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:112:in'reload'

来自/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1250:in'test_case'

我的问题是:如何告诉Rails使用我的TestCase类而不是ActiveRecord :: TestCase。

TestCase类:

class TestCase < ActiveRecord::Base

  set_table_name "test_case"

  has_many   :test_results
  belongs_to :component,         :foreign_key => "subsystem_id"
  belongs_to :domain,            :foreign_key => "area_id"
  belongs_to :use_case,          :foreign_key => "use_case_id"
end

2 个答案:

答案 0 :(得分:1)

怎么样

class TestResult < ActiveRecord::Base
  belongs_to :test_case, :class_name => "::TestCase"
end

答案 1 :(得分:0)

怎么样

class TestResult < ActiveRecord::Base
  belongs_to :test_case
end