dimanche 27 mars 2016

Creating a rails relationship object in a has_many through association

I have 3 models A, B and C with the following relationship:

A has_many :Bs
A has_many :Cs, through: :Bs, source: :C

Now i want to create a relationship object of model B like this:

@B = A.Bs.create!(B_params)

which does create an object of B with the correct A_id and C_id, however calling A.Cs does not return anything. It works if i do something like A.Cs << C, but after adding additional columns to the relationship model, I'd like to create the middle man directly, but still access the has_many part directly. Any options in the model to achieve that directly or what would be the 'best' workaround (something like

A.Cs << C
A.B.last.update_attributes(B_params)

)?

Aucun commentaire:

Enregistrer un commentaire