[英]uninitialized constant NameError when use has_many
I practice my RoR skills and try develop application to already created DB. 我练习我的RoR技能,并尝试将应用程序开发到已经创建的数据库中。 It's have 4 tables: testplans , testplan_tcversions ,* test_project* and nodes . 它有4个表: testplans , testplan_tcversions ,* test_project *和node 。
I'm code 2 models for this tables: 我为此表编写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
and 和
class TestSuite < ActiveRecord::Base
self.table_name='testplan_tcversions'
belongs_to :test_plan
has_one :node, foreign_key: id, inverse_of: :test_collection
end
But I get exception uninitialized constant TestPlan::TestSuite when try: @suits=TestPlan.find(4906).test_suites
但是当尝试时,我得到了未初始化的常量TestPlan :: TestSuite异常: @suits=TestPlan.find(4906).test_suites
I found a lot of answers that Models must singular and table must plural, but my Models names are singular, names of tables I point in self.table_name. 我发现了很多答案,其中模型必须为单数,表必须为复数,但是我的模型名称为单数,我指向self.table_name的表名称。
What I did wrong? 我做错了什么?
UPD UPD
This my db:schema:dump 这是我的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
How are your migrations set up? 您的迁移方式如何设置?
If they are set up correctly, the relationship between TestSuite and TestPlan should look like this: 如果设置正确,则TestSuite和TestPlan之间的关系应如下所示:
class TestPlan < ActiveRecord::Base
has_many :test_suites
end
class TestSuite < ActiveRecord::Base
belongs_to :test_plan
end
For this to work though, your TestSuite migration needs to have a test_plan_id column. 为了使此工作正常进行,您的TestSuite迁移需要有一个test_plan_id列。 That should look like this. 看起来应该像这样。
class TestSuite < ActiveRecord::Migration
belongs_to :test_plan
end
If this is set up correctly, you should then be able to call @suits=TestPlan.find(4906).test_suites
. 如果正确设置,则应该可以调用@suits=TestPlan.find(4906).test_suites
。
Make sure your table names correspond to your model names. 确保您的表名称与您的模型名称相对应。 If you don't have a table named 'testplan_tcversions', the association isn't going to work. 如果您没有名为“ testplan_tcversions”的表,则该关联将无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.