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".
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.
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
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.
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