2013-04-18 62 views
0

比方說,我有Profile Model,Test Model和TestItem模型。 配置文件有很多test_items,Test有一個test_item。 (因此,當我們將TestItem添加到配置文件中時,它不是新的Test,就像我們將LineItem添加到購物車時一樣)不是新產品)。有效記錄關係問題

我想配置文件有很多TestItems,但我需要指定,這個TestItems應該屬於不同的測試。我怎麼做?

回答

3
class Profile 
    has_many :test_items 
end 

class TestItems 
    belongs_to :profile 
    belongs_to :test 
end 

class Test 
    attr_accessible :test_item 
end 
+0

你的意思是我需要刪除has_one?你能解釋一下它能達到什麼目的嗎? –

+0

你可以有has_one而不是attr_accessible。事實上它可能會更好。但總體上這種關係應該可以達到你所期望的。 – Dorian