# Write\_attribute functionality for related objects?

**URL:** <https://www.ruby-forum.com/t/write-attribute-functionality-for-related-objects/111121>\
**Category:** Rails\
**Created:** [August 2, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/write-attribute-functionality-for-related-objects/111121 "2007-08-02T21:04:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![morten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@morten](https://www.ruby-forum.com/u/morten)\
**Post date:** [August 2, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/write-attribute-functionality-for-related-objects/111121/1 "2007-08-02T21:04:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![morten](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@morten](https://www.ruby-forum.com/u/morten)\
**Post date:** [August 2, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/write-attribute-functionality-for-related-objects/111121/2 "2007-08-02T21:19:12Z")

</div>

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