user1611830
user1611830

Reputation: 4867

nested forms and nested_attributes issue

Suppose I have 3 models A B C, with

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

and in my view I have some nested forms

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

but I get an error

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

What did I do bad ?

Upvotes: 1

Views: 62

Answers (2)

MurifoX
MurifoX

Reputation: 15109

I think there is a has_many :Cs missing on class A.

Upvotes: 1

epsilones
epsilones

Reputation: 11629

I think you should add :

    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

Upvotes: 1

Related Questions