Skip to content Skip to sidebar Skip to footer

Edit/update Collection_select With Has-many-through Association

I have the following models: RECIPE, TAG and TAGGING (join table) recipe.rb has_many :taggings has_many :tags, through: :taggings accepts_nested_attributes_for :taggings tag.rb ha

Solution 1:

Try with

= f.fields_for :taggings, @recipe.taggings do|builder|

Instead of

= f.fields_for :taggings, @recipe.taggings.build do|builder|

Because you are returning new tagging everytime instead of re-using the previously created ones.

Solution 2:

It worked for me:

= f.fields_for :taggings, @recipe.taggings.order("id").first do|diet|
    = diet.collection_select :tag_id, Tag.diet, :id, :name

= f.fields_for :taggings, @recipe.taggings.order("id").second do|category| 
    = category.collection_select :tag_id, Tag.category, :id, :name

= f.fields_for :taggings, @recipe.taggings.order("id").third do|festivity| 
    = festivity.collection_select :tag_id, Tag.festivity, :id, :name

= f.fields_for :taggings, @recipe.taggings.order("id").fourth do|daily| 
    = daily.collection_select :tag_id, Tag.daily, :id, :name

Post a Comment for "Edit/update Collection_select With Has-many-through Association"