Basically, when I add a step to a new exercise’s “steps” array, it
saves, and everything works. If I then get the exercise from the model
(via find), clear its array, then add the step again, I cannot save
the step to the exercise. Here is a console dump which shows exactly
what happens:
What is going on here? This is so anti-intuitive - how could a bug
like this exist and not have been detected hundreds of times before?
Yes, I would regard this as a Rails bug.
The clear method is not setting the foreign key to
nil on the in-memory object.
Even if it were setting it on the in memory object it wouldn’t be in
memory object you think it is. Because a was reloaded, the objects in
a.steps are not the objects b & c (although they do correspond to the
same database rows).
Even if it were setting it on the in memory object it wouldn’t be in
memory object you think it is. Because a was reloaded, the objects in
a.steps are not the objects b & c (although they do correspond to the
same database rows).
The problem happens before a is reloaded.
I just did some experiments with puzzling results.
Would you be able to help explain them Fred?
Why does only the first object in the collection
have a different object_id, and why does this
difference go away if the empty a.steps is loaded
before b is added? Is it a proxy thing?
I added a few lines to the has_many code to set
the foreign keys on the in-memory records to nil.
But because of the above behaviour the fk doesn’t change
on the local variable that held the first element added to
the collection, unless the collection had been pre-loaded.
Why does only the first object in the collection
have a different object_id, and why does this
difference go away if the empty a.steps is loaded
before b is added? Is it a proxy thing?
Because .first will hit the database and load a fresh copy (if the
collection is not already loaded). I waffled about this a bit a while
ago: First, foremost and [0] - Space Vatican
Even if it were setting it on the in memory object it wouldn’t be in
memory object you think it is. Because a was reloaded, the objects in
a.steps are not the objects b & c (although they do correspond to the
same database rows).
The problem happens before a is reloaded.
I just did some experiments with puzzling results.
Would you be able to help explain them Fred?
The association_collection << method could easily be changed
to respect the identity of objects being added to a collection.
Should this be done to respect the POLS, violated for the OP?
Possibly. This area is fraught with subtleties though.
Why does only the first object in the collection
have a different object_id, and why does this
difference go away if the empty a.steps is loaded
before b is added? Is it a proxy thing?
Because .first will hit the database and load a fresh copy (if the
collection is not already loaded). I waffled about this a bit a while
ago: First, foremost and [0] - Space Vatican
Thanks Fred for pointing out the new behaviour of
“first” and “last”.
However that’s not the problem here. You get the same
behaviour if you use steps[0].
Looking at it again after some sleep, I see that the
different b object is actually loaded at the “a.steps << b”
line. That is, the object is added by updating the foreign key
in the database, loading the collection, then folding in any
new_records, which ends up giving you the different b from
the database.
The association_collection << method could easily be changed
to respect the identity of objects being added to a collection.
Should this be done to respect the POLS, violated for the OP?
So is this a known bug? Is this fixed in 2.2.2? I’m currently running
rails 2.1.1.
The issue is present in 2.2.2, and probably edge.
I’m looking into a fix, but as Fred says, there are
several subtleties. I suggest you watch this ticket:
Assuming your console log was an attempt to investigate a problem
with some real app code, you should be able to work around the
problem by eliminating some unnecessary saves and some object
reuse. You can post your real code if you like.