Sunday, November 27, 2011

[Rails] one_to_many relationship, auto update?

Hi,

I am new to Ruby on Rail. I have a question regarding the one_to_many
relationship which doesn't automatically update the relationship by
Rails.

Support I have two classes: Car and Wheel.

Car has many Wheel where as Wheel belongs to Car. Therefore, I declare
the classes as below:

// car.rb
class Car < ActiveRecord::Base
has_many :wheels
end

// wheel.rb
class Wheel < ActiveRecord::Base
belongs_to :car
end

The is how I test drive in rails console:

car1 = Car.new
car1.save

car2 = Car.new
car2.save

wheel1 = Wheel.new
wheel1.save

wheel2 = Wheel.new
whee2.save

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?

Thanks.

--
Posted via http://www.ruby-forum.com/.

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate