Forum: Ruby on Rails write_attribute functionality for related objects?

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.
Morten (Guest)
on 2007-08-02 23:04
(Received via mailing list)
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
Morten (Guest)
on 2007-08-02 23:19
(Received via mailing list)
> 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?
This topic is locked and can not be replied to.