Write_attribute functionality for related objects?


#1

Hi. Given:

class Group < AR
has_many :users
end

Is it in any way possible to retain reference to the old users when
they get replaced using group.users=(new_users) ?

The below doesn’t work but illustrates what I’m looking for:

class Group < AR
has_many :users
def users=(new_users)
@old_users = users
write_attribute(‘users’, new_users)
end
end

I need to examine the differences when related objects (in this case
users) change.

Morten


#2

The below doesn’t work but illustrates what I’m looking for:

class Group < AR
has_many :users
def users=(new_users)
@old_users = users
write_attribute(‘users’, new_users)
end
end

I’m currently doing something like this:

class Group < AR
has_many :users
def validate_on_update
old_users = Group.find(id).users
#Do the checks here…
end
end

But that’s nasty. Anyone know if there’s a plugin that allows AR to
contain a version of the object in the state it had as it got read
from the DB for later comparison?