# Validate\_on\_update compare old value to new value

**URL:** <https://www.ruby-forum.com/t/validate-on-update-compare-old-value-to-new-value/131050>\
**Category:** Rails\
**Created:** [March 5, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/validate-on-update-compare-old-value-to-new-value/131050 "2008-03-05T16:23:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 5, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/validate-on-update-compare-old-value-to-new-value/131050/1 "2008-03-05T16:23:59Z")

</div>

Hello,  
After a model has been saved I want to prevent some of it’s fields  
from being updated. I was going to do this by overriding  
validate\_on\_update but I don’t know how to access the old value to  
compare it to the new value.  
Cheers,  
Ewan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 5, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/validate-on-update-compare-old-value-to-new-value/131050/2 "2008-03-05T17:35:52Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hello,  
> After a model has been saved I want to prevent some of it’s fields  
> from being updated. I was going to do this by overriding  
> validate\_on\_update but I don’t know how to access the old value to  
> compare it to the new value.  
> Cheers,  
> Ewan

How about overriding the setter methods for those fields you want to  
lock. Something like…

def title=(t)  
self[:title] = t if self.new\_record?  
end

–  
[http://www.5valleys.com/](http://www.5valleys.com/)  
[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 6, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/validate-on-update-compare-old-value-to-new-value/131050/3 "2008-03-06T19:09:48Z")

</div>

Hello Jon,  
Thanks for the reply.  
I made the attribute read only doing

attr\_readonly :title

Ewan
