0

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 for 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 for parent:

= f.simple_fields_for :children, @children do |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 is that when Parent is saved, children_parents join table is update (nice), children table has no id children inserted (nice), BUT if the children already has an ID (persisted), children table does not perform the update on them

Debugging the prev line before @parent.save, is possible to ensure that @parent.children contains the children new attributes. Executed the command @parent.save, children attributes aren't updating , but if save is called again, it performs the children update.

What could be happening?

Mauro Dias
  • 1,083
  • 1
  • 11
  • 23
  • Why do you have the `child_ids` in your `parent_params`? – nathanvda Nov 11 '16 at 16:21
  • @nathanvda. Otherwise, create fails with, i.E, `@parent = current_user.parents.build(child_params)` raises `Couldn't find Child with ID=XXX for Parent with ID=` – Mauro Dias Nov 14 '16 at 11:07
  • Shouldn't that be `current_user.parents.build(parent_params)` ?? Can you show your controller code where you save it? That is not how you save an element with nested attributes. Imho the `child_ids` is not needed and the cause of your error (because 1 operation sets the nested children with content, and the other sets the nested children as references --thus losing the content-changes). – nathanvda Nov 14 '16 at 12:16
  • you are right, I mistyped (its `parent_params`), I'm going to edit the question with the controller code. – Mauro Dias Nov 14 '16 at 12:24
  • like you see the inclusion of a default child is necessary in parent creation @nathanvda – Mauro Dias Nov 14 '16 at 12:37
  • Just notice that outside fields_for file (child_fields), children has the user_id persisted, but inside this file, f.object returns a child with user_id 0 – Mauro Dias Nov 14 '16 at 12:58
  • You are doing something weird in your `new` action. Also: sometimes you write `child_ids`, sometimes `children_ids`, you set/show a `user_id` yet it is not in your strong-parameters? You create a child dependent on the user before the parent? Very hard to be make sense of it all and be of help. Did you check the demo-project https://github.com/nathanvda/cocoon_simple_form_demo ? – nathanvda Nov 14 '16 at 13:26
  • it already was, just omitted coz I thought user would has no relation with the initial trouble – Mauro Dias Nov 14 '16 at 13:43

0 Answers0