On 27 November 2011 22:34, Dave Aronson
<googlegroups2dave@davearonson.com> wrote:
> On Sun, Nov 27, 2011 at 16:27, kahou l. <lists@ruby-forum.com> 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
It is not appropriate to set the id values manually. Save them
instead, then rails will allocate the ids.
> 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
Again, don't set the ids manually, let rails do it.
> => [#<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.
Try again with letting rails manage the ids and you may get a different result.
Colin
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment