I have a models Parent and Child, an the following association:

Parent
  has_many :children, dependent: :destroy

Child
  belongs_to :parent

Further, I have two Parent instances:

  pfrom = Parent.find_by_id(from_id)
  pto = Parent.find_by_id(to_id)

My goal is to transfer all children from pto to pfrom, and then delete
pto.

The first part seems to be easy:

  pfrom.children.each { |ch| ch.update_attributes!(parent_id: pto.id }

If I run *only* this code, I can see that pto indeed contains now the
children formerly belonging to pfrom, and iterating over pfrom shows
that there are no children.

HOWEVER, if I add the following line:

  pfrom.destroy

I can see (from the SQL statements which are issued by this call), that
all the former pfrom children are deleted!

It somehow seems as if this information has been "cached". Could this be
the case? How then would I correctly implement the "move".

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/f24f6034906a184a40a102fa92a2dc22%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to