2013-08-21 43 views
1

假設我有3種型號A B C,與嵌套形式和nested_attributes問題

class A 
    has_many :Bs, through: :Cs 
    accepts_nested_attributes_for :Cs 
end 

class B 
    has_many :As, through: :Cs 
end 

class C 
    belongs_to :A 
    belongs_to :B 
end 

在我看來,我有一些嵌套形式

= form_for @A do |f| 
... 
    = f.fields_for :Cs do |builder| 
    ... 

,但我得到一個錯誤

ArgumentError (No association found for name `C'. Has it been defined yet?) 

我做了什麼壞事?

回答

1

我認爲你應該加上:

class A 
     has_many :Cs 
     has_many :Bs, through: :Cs 
     accepts_nested_attributes_for :Cs 
    end 

    class B 
     has_many :Cs 
     has_many :As, through: :Cs 
    end 
1

我覺得有一個has_many :Cs失蹤class A