[英]NameError (uninitialized constant ApplicationRecord) in has_many through association
[英]uninitialized constant NameError when use has_many
我练习我的RoR技能,并尝试将应用程序开发到已经创建的数据库中。 它有4个表: testplans , testplan_tcversions ,* test_project *和node 。
我为此表编写2个模型的代码:
class TestPlan < ActiveRecord::Base
self.table_name= 'testplans'
belongs_to :test_project
has_many :test_suites, foreign_key: :testplan_id, inverse_of: :test_plan
has_one :node, foreign_key: :id, inverse_of: :test_plan
end
和
class TestSuite < ActiveRecord::Base
self.table_name='testplan_tcversions'
belongs_to :test_plan
has_one :node, foreign_key: id, inverse_of: :test_collection
end
但是当尝试时,我得到了未初始化的常量TestPlan :: TestSuite异常: @suits=TestPlan.find(4906).test_suites
我发现了很多答案,其中模型必须为单数,表必须为复数,但是我的模型名称为单数,我指向self.table_name的表名称。
我做错了什么?
UPD
这是我的db:schema:dump
create_table "testplans", force: true do |t|
t.integer "testproject_id"
t.text "notes"
t.integer "active"
t.integer "is_open"
t.integer "is_public"
t.text "api_key"
end
create_table "testplan_tcversions", force: true do |t|
t.integer "testplan_id"
t.integer "tcversion_id"
t.integer "node_order"
t.integer "urgency"
t.integer "platform_id"
t.integer "author_id"
t.datetime "creation_ts"
end
您的迁移方式如何设置?
如果设置正确,则TestSuite和TestPlan之间的关系应如下所示:
class TestPlan < ActiveRecord::Base
has_many :test_suites
end
class TestSuite < ActiveRecord::Base
belongs_to :test_plan
end
为了使此工作正常进行,您的TestSuite迁移需要有一个test_plan_id列。 看起来应该像这样。
class TestSuite < ActiveRecord::Migration
belongs_to :test_plan
end
如果正确设置,则应该可以调用@suits=TestPlan.find(4906).test_suites
。
确保您的表名称与您的模型名称相对应。 如果您没有名为“ testplan_tcversions”的表,则该关联将无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.