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.
It's only being manifest though because of the
unnecessary saves you're doing, meaning that
the problem has probably been rarely seen.
--
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com
I take this back:
> It's only being manifest though because of the
> unnecessary saves you're doing, meaning that
> the problem has probably been rarely seen.
The problem has been exposed by the new ActiveRecord
change detection code.
> 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?
Loading development environment (Rails 2.2.2)
>> a = Exercise.find(1)
=> #<Exercise id: 1>
>> b = Step.new
=> #<Step id: nil, exercise_id: nil>
>> b.save
=> true
>> c = Step.new
=> #<Step id: nil, exercise_id: nil>
>> c.save
=> true
>> a.steps << b
=> [#<Step id: 19, exercise_id: 1>]
>> a.steps << c
=> [#<Step id: 19, exercise_id: 1>, #<Step id: 20, exercise_id: 1>]
>> b.object_id
=> 70254505869500
>> a.steps.first.object_id
=> 70254505813160
>> c.object_id
=> 70254505838840
>> a.steps.last_object_id
=> 70254505838840
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.
Just a WAG, but
a.steps.first.object_id isn't the same reference method as
a.steps.last_object_id
Is the first.object_id creating a new reference to that object, whereas
last_object_id is returning the reference present in a.steps?
Try the experiment again with the same object referencing method used
for both first and last.
--
Posted via http://www.ruby-forum.com/.
>> 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: http://www.spacevatican.org/2008/11/15/first-foremost-and-0
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?
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:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1706
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.