On Sun, Nov 27, 2011 at 16:27, kahou l. [email protected] wrote:
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)