2013-08-21 38 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