It seems like rails is doing some data caching on objects
that is causing inconsistencies. If you do a lookup through
a belongs-to, that result persists even if you change the foreign
key (on the memory object without yet saving):
parent has-many children
child belongs-to parent
Parent.find(1).surname
=> "Smith"
Parent.find(2).surname
=> "Jones"
c = Child.find(1)
c.parent_id
=> 1
c.parent_id = 2
c.parent.surname
=> "Jones"
Okay, fine. What I wanted. Change has taken
effect. But now:
c.parent_id = 1
c.parent.surname
=> "Jones"
Not what I wanted. The earlier result is cached somewhere.
(I did the original id change from 1 to 2 just to show that
the cached value was *not* just whatever the initial database
value was). It's not a matter of "what's in the db versus
what has not been saved yet". It's more arbitrary. Whatever
value c.parent.surname reports is dependent on when you first
did a lookup, and is "stuck" from then on.
So you get weirdness like this:
c.parent.surname
=> "Jones"
Parent.find(c.parent_id).surname
=> "Smith"
Is this acceptable? If so, how does this not mean I need to ditch the idiom
"c.parent.surname" (nice) in favor of "Parent.find(c.parent_id).surname"
in any of my code where a value *might* have changed and it's possible
I *might* have done accessed through the association since then, and I
haven't saved the child record yet (for good reason, maybe I'm doing this
as part of validation)?
Is there a way to force rails to not use (or mark as out-of-date)
the cached info? A "c.reload" does not work, because my c object
in memory may deliberately have different info than the database,
and the reload would wipe it.
Thanks in advance!
-glenn
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby