On Jul 17, 2014, at 7:27 AM, Ronald Fischer wrote:
> 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".
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 [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.
--
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/57D3BB16-E791-4EF9-A4E5-A332470897B6%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.