Forum: Ruby on Rails active record associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
98208c10adcf9b7bd9cbe6c23ec2c50d?d=identicon&s=25 Franz Obenhauser (obenhauser)
on 2008-11-07 05:18
(Received via mailing list)
What's wrong with ruby associations?


Somehow association fields don't have any support for dirty objects
nor optimistic locking.

I would expect dirty objects to work like this:

class Person
  has_many :cars
end

>> person = Person.find_by_name("Jack")
>> person.cars

=> [#<Car id: 2, name: "BMW", created_at: "2008-11-04 03:04:46",
updated_at: "2008-11-04 03:04:46">]
>> person.cars=[Car.find_by_name("Porsche"), Car.find_by_name("VW")]
>> person.cars

=> [#<Car id: 1, name: "Porsche", created_at: "2008-11-04 03:04:46",
updated_at: "2008-11-04 03:04:46">, #<Car id: 3, name: "VW",
created_at: "2008-11-04 03:04:46", updated_at: "2008-11-04 03:04:46">]
>> person.changed?
=> true
>> person.changes

=> {"cars"=>[[#<Car id: 2, name: "BMW", created_at: "2008-11-04
03:04:46", updated_at: "2008-11-04 03:04:46">], [#<Car id: 1, name:
"Porsche", created_at: "2008-11-04 03:04:46", updated_at: "2008-11-04
03:04:46">, #<Car id: 3, name: "VW", created_at: "2008-11-04
03:04:46", updated_at: "2008-11-04 03:04:46">]]}

>> person.save  #associations are saved in the database

However ror returns the following

>> person = Person.find_by_name("Jack")
>> person.cars

=> [#<Car id: 2, name: "BMW", created_at: "2008-11-04 03:04:46",
updated_at: "2008-11-04 03:04:46">]
>> person.cars=[Car.find_by_name("Porsche"), Car.find_by_name("VW")]  #associations are 
saved in the database
>> person.cars

=> [#<Car id: 1, name: "Porsche", created_at: "2008-11-04 03:04:46",
updated_at: "2008-11-04 03:04:46">, #<Car id: 3, name: "VW",
created_at: "2008-11-04 03:04:46", updated_at: "2008-11-04 03:04:46">]
>> person.changed?
=> false
>> person.changes

=> {}

The same problem happens with optimistic locking

p1 = Person.find_by_name("Jack")
p2 = Person.find_by_name("Jack")

p1.cars = [Car.find_by_name("Porsche")]
p1.save

p2.cars = [Car.find_by_name("BMW")]
p2.save

I would expect the object to raise an exception, but it doesn't.

Isn't there a way to unify the behaviour of regular fields and
associations?  Most objects in my app don't represent flat tables, but
tables that associate each other.  Right now I'm forced to having to
wrap many attribute updates into a transaction and write my own object
comparisons.
This topic is locked and can not be replied to.