ruby on rails - Creating a Parent object whose HABTM accepts_nested_attributes_for children is ignoring the association(children) attributes update -


my activerecord models:

class parent < ar   has_and_belongs_to_many :children   accepts_nested_attributes_for :children, reject_if: :all_blank, allow_destroy: true end  class child < ar   has_and_belongs_to_many :parents end 

the controller parent creating. [edited]

def new   current_child = current_user.set_child #first or initialize   @parent = current_user.parents.build   @parent.child_ids = [current_child.id].compact   @children = [current_child] end  def create   @parent = current_user.parents.build(parent_params)   if @parent.save     ...   else     @children = @parent.children     render :new   end end 

[/edited]

def parent_params   params.require(:parent).merge( child_ids: params[:parent][:children_attributes].map{|p,v| v[:id]} ).   permit(:id, :picture, :remove_picture, child_ids: [],   children_attributes: [ :id, :user_id, :full_name, :_destroy ] ) end 

[edited]

the simple form parent:

= f.simple_fields_for :children, @children |child|    = render 'child_fields', f: child 

child fields file:

= f.hidden_field :id, value: f.object.id = f.hidden_field :user_id, value: f.object.user_id ... 

[/edited]

the point when parent saved, children_parents join table update (nice), children table has no id children inserted (nice), but if children has id (persisted), children table not perform update on them

debugging prev line before @parent.save, possible ensure @parent.children contains children new attributes. executed command @parent.save, children attributes aren't updating , if save called again, performs children update.

what happening?


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -