# Getting old values

**URL:** <https://www.ruby-forum.com/t/getting-old-values/53934>\
**Category:** Rails\
**Created:** [February 17, 2006, 9:55am UTC](https://www.ruby-forum.com/t/getting-old-values/53934 "2006-02-17T09:55:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [February 17, 2006, 9:55am UTC](https://www.ruby-forum.com/t/getting-old-values/53934/1 "2006-02-17T09:55:58Z")

</div>

Hi all,

I’ve add :before\_update event to my model  
Before updating a record I want to check if new attributes differ to old  
attr.

My question is how to retrieve old attributes in :before\_update event ?

thanks

–  
gratis egold 1$ [http://shegold.com/](http://shegold.com/)

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [February 17, 2006, 10:29am UTC](https://www.ruby-forum.com/t/getting-old-values/53934/2 "2006-02-17T10:29:10Z")

</div>

Hello,

On Fri, Feb 17, 2006 at 03:53:06PM +0700, Superbiji wrote:

> My question is how to retrieve old attributes in :before\_update event ?

I asked the same question some days ago. The answer was use the  
database.  
The database is made to be queried and queries are cached. So just do a  
MyModel.find(foo.id).bar

Hope this helps!

Regards,

–  
Damien MERENNE [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.cosinux.org/blogs/dam/](http://www.cosinux.org/blogs/dam/)

Chuck Norris doesn’t churn butter.  
He roundhouse kicks the cows and the butter comes straight out.

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [February 20, 2006, 11:09am UTC](https://www.ruby-forum.com/t/getting-old-values/53934/3 "2006-02-20T11:09:01Z")

</div>

On 2/17/06, Damien MERENNE [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I asked the same question some days ago. The answer was use the database.  
> The database is made to be queried and queries are cached. So just do a  
> MyModel.find(foo.id).bar

Thanks for your answer, I’ll try that

I do different way,  
by overiding all attributes with  
def ATTRIBUTE=(value)  
@oldies[:attribute] = value  
self[:attribute] = value  
end

but I hope “getting old values” should be ActiveRecord’s standard  
feature

–  
jual beli gratis egold 1$ [http://shegold.com/](http://shegold.com/)

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [February 20, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/getting-old-values/53934/4 "2006-02-20T19:13:02Z")

</div>

What exactly are you checking for?

ActiveRecord already has built in validation for uniqueness, take a  
look at validates\_uniqueness\_of (pg 274 of AWD)

class User \< ActiveRecord::Base  
validates\_uniqueness\_of :name,  
:on =\> :save,  
:message =\> “is already being used”

And you can always call validate whenever you want, not just upon save.

On 2/20/06, Superbiji [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> by overiding all attributes with
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- Nic

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [February 21, 2006, 7:57am UTC](https://www.ruby-forum.com/t/getting-old-values/53934/5 "2006-02-21T07:57:55Z")

</div>

On 2/21/06, Nic W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What exactly are you checking for?

I want to make sure ONE field is really modified before update.  
for example, a record can’t be modified if field status is not changed

–  
Jual Beli Gratis egold 1$ [http://shegold.com/](http://shegold.com/)

---

<div class="post-metadata">

**Author:** ![Superbiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Superbiji](https://www.ruby-forum.com/u/Superbiji)\
**Post date:** [February 21, 2006, 1:23pm UTC](https://www.ruby-forum.com/t/getting-old-values/53934/6 "2006-02-21T13:23:21Z")

</div>

Superbiji wrote:

> On 2/21/06, Nic W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > What exactly are you checking for?
> 
> I want to make sure ONE field is really modified before update.  
> for example, a record can’t be modified if field status is not changed  
> Not to dispute your need for this, but what’s the use case here? Why  
> does it matter if an unchanged record is saved?
