ActiveRecord: moving all children to a new parent

67 views
Skip to first unread message

Ronald Fischer

unread,
Jul 17, 2014, 7:28:11 AM7/17/14
to rubyonra...@googlegroups.com
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/.

Walter Lee Davis

unread,
Jul 17, 2014, 9:15:31 AM7/17/14
to rubyonra...@googlegroups.com
Reload the parent that you wish to destroy before you destroy it. Also, maybe it would be enough to set the "dead" parent's children array to []. While the key is stored only on one side, when the parent record is initialized, its children are instantiated in memory. You are correct, they are cached.

Walter

>
> --
> 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 rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> 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.

Ronald Fischer

unread,
Jul 18, 2014, 2:24:59 AM7/18/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1152668:
> On Jul 17, 2014, at 7:27 AM, Ronald Fischer wrote:
>> pfrom = Parent.find_by_id(from_id)
>> children formerly belonging to pfrom, and iterating over pfrom shows
>> the case? How then would I correctly implement the "move".
> Reload the parent that you wish to destroy before you destroy it.

I was not aware of the reload method! Thank you for pointing this out.
So this would be

pfrom.reload.destroy

> Also,
> maybe it would be enough to set the "dead" parent's children array to
> [].

Interesting idea. I think, 'reload' is nicer, because it is more likely
that this part of the interface won't change when a new version of Rails
is coming. Changing the children-array looks a bit like a hack to me (we
need to know that they are stored in an array). But still I'm curious:
How do I explicitly manipulate the childrens array? I didn't find a
suitable method in the Active Record docs, and I don't expect that
something like

pfrom.children=[]

would do it.

Ronald

Matt Jones

unread,
Jul 18, 2014, 8:13:21 AM7/18/14
to rubyonra...@googlegroups.com
That's actually *exactly* what will do it. :)

Running that will do a single UPDATE query to set all the involved parent_id columns to NULL. So one way to implement the swap is:

saved_children = pfrom.children.to_a
pfrom.children = []
pto.children = saved_children

NOTE: this won't work if there are children on pto already. For that, try `pto.children = pto.children + saved_children`.

--Matt Jones 

Ronald Fischer

unread,
Jul 18, 2014, 1:36:17 PM7/18/14
to rubyonra...@googlegroups.com
I am stunned!!!!!

Every day I like Rails more....

Thanks a lot!

Ronald

Ronald Fischer

unread,
Jul 23, 2014, 8:23:25 AM7/23/14
to rubyonra...@googlegroups.com
Matt Jones wrote in post #1152815:
> On Friday, 18 July 2014 02:24:59 UTC-4, Ruby-Forum.com User wrote:
> saved_children = pfrom.children.to_a
> pfrom.children = []
> pto.children = pto.children + saved_children.

I finally found the time to rewrite this part of my application
according to this suggestion, but I now get an error "can't modify
frozen Hash", when I try to add the saved children. The actual code
which I am using is here:

# tempdict is "pfrom" and targetdict is "pto"
targetdict=Dict.find_by_id(....)
# creating and saving a tempdict together with several children,
i.e. cards
tempdict=Dict.new(....)
tempdict.save!
# Code for creating and adding the children omitted for brevity
....
cards_to_add=tempdict.cards.to_a
tempdict.cards=[]
targetdict.cards += cards_to_add

I find it strange that I get the error on the last line. The error
message is usually an indication that I am trying to save something
which has been deleted already, but in this case, no deletion had been
done on 'targetdict' before.

Any idea, where this error could come from?

Frederick Cheung

unread,
Jul 23, 2014, 9:57:20 AM7/23/14
to rubyonra...@googlegroups.com


  pfrom.children=[]

would do it.


That's actually *exactly* what will do it. :)

Running that will do a single UPDATE query to set all the involved parent_id columns to NULL. So one way to implement the swap is:

saved_children = pfrom.children.to_a
pfrom.children = []
pto.children = saved_children


This will depend on what you have set the :dependant option on the association to - it would do this if you have set the option to nullify (the default), but it would destroy the children if it was :destroy or :delete_all


Fred

Ronald Fischer

unread,
Jul 24, 2014, 2:55:07 AM7/24/14
to rubyonra...@googlegroups.com
Ah, I have set it to :destroy!

But why do I get the error when I assign to pto.children? I would expect
such an error then when pfrom is deleted.

Ronald

Matt Jones

unread,
Jul 24, 2014, 3:19:42 PM7/24/14
to rubyonra...@googlegroups.com
You tell me; you're the one who can see the stack trace. :)

Maybe something that it's modifying has been destroyed?

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages