On Friday, 18 July 2014 02:24:59 UTC-4, Ruby-Forum.com User wrote: > > 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. > > 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 -- 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-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/299446fe-8bd9-4401-9dc7-46c9cb338d98%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.