2014-08-29 57 views
0

我练习了我的RoR技能并尝试将应用程序开发为已创建的数据库。它有4个表格:testplans,testplan_tcversions,* test_project *和节点使用has_many时未初始化的常量NameError

我的代码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 

,但我得到异常未初始化的不断测试计划:: TestSuite的时候尝试:@suits=TestPlan.find(4906).test_suites

我找到了很多答案,模型必须单数,表格必须是复数,但我的模型名称是单数,表格名称指向self.table_name。

我做错了什么?

UPD

这是我的DB:架构:甩

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 

回答

0

如何在您的迁移设置?

如果设置正确,TestSuite的测试计划和之间的关系应该是这样的:

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'的表,该关联不起作用。

+0

我使用已经创建的数据库,并没有写入迁移,因为数据库拥有所有包含字段和数据的表。 TestSuite有'_test_plan_id_列,但是_testplan_id_并且我指向'foreign_key::testplan_id'。表名不对应模型名称,我使用'self.table_name =' – 2014-08-30 05:13:32

相关问题