active record associations

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.