On Sun, Nov 27, 2011 at 16:27, kahou l. <[email protected]> wrote:

> car1.wheels << wheel1
>
> car1.wheels << wheel2
> car2.wheels << wheel2
>
> Note that car1 wheels STILL contains wheel2 when I reassign wheel2 to
> car2.
> I expect car1 doesn't have wheel2 anymore...
>
> I don't know why it doesn't get update automatically that car1 shouldn't
> contains wheel2 anymore. Can anybody help me to solve this problem?

Interesting.  What's *supposed* to happen is that wheel's car_id gets
set to the id of the car.  When you do that the second time, that
*should* overwrite the first one, so that the first one's wheels would
no longer include it.  Why that's not happening, I'm not sure.  Just
tried it with one of my own apps, that has Decisions that belong to
Users.  Transcript:

 ruby-1.9.3-head :005 >   u1 = User.new
  => #<User id: nil, other details snipped>
 ruby-1.9.3-head :006 > u1.id = 1001
  => 1001
 ruby-1.9.3-head :007 > u2 = User.new
  => #<User id: nil, other details snipped>
 ruby-1.9.3-head :008 > u2.id = 1002
  => 1002
 ruby-1.9.3-head :009 > d = Decision.new
  => #<Decision id: nil, user_id: nil, other details snipped>
ruby-1.9.3-head :020 > d.id = 1000
 => 1000
 ruby-1.9.3-head :010 > u1.decisions << d
  => [#<Decision id: 1000, user_id: nil, other details snipped>]
 ruby-1.9.3-head :011 > u2.decisions << d
  => [#<Decision id: 1000, user_id: nil, other details snipped>]
ruby-1.9.3-head :016 > d
 => #<Decision id: 1000, user_id: nil, other details snipped>
ruby-1.9.3-head :021 > u1.decisions
 => [#<Decision id: 1000, user_id: nil, other details snipped>]
ruby-1.9.3-head :022 > u2.decisions
 => [#<Decision id: 1000, user_id: nil, other details snipped>]

How odd, user_id stays nil!  Given that, I'm not sure just how Rails
"knows" that u#.decisions includes d.  If anyone here has a clue,
maybe that would clear things up.

Meanwhile, as Colin said, Rails objects usually have a very short
lifespan, so maybe the fact that it was in the console, rather than
over the course of a bunch of HTTP requests, could have something to
do with it.  Maybe something gets set when Rails set up or tears down
a request object, that isn't happening here.  Try making an app in
which you can CRUD cars and wheels, put wheels on cars, and take
wheels off cars.  Let us know what happens when you try to do the same
thing through the app.

-Dave

-- 
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to