# Reading Model data before update

**URL:** <https://www.ruby-forum.com/t/reading-model-data-before-update/123240>\
**Category:** Rails\
**Created:** [December 15, 2007, 10:36am UTC](https://www.ruby-forum.com/t/reading-model-data-before-update/123240 "2007-12-15T10:36:26Z")\
**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:** [December 15, 2007, 10:36am UTC](https://www.ruby-forum.com/t/reading-model-data-before-update/123240/1 "2007-12-15T10:36:26Z")

</div>

Hi all,

here is the challenge:

I have this validation method, that basically prevents an object which  
billed = true to be updated:

def before\_update  
if read\_attribute(:billed)  
errors.add\_to\_base(‘This service has already been billed for’)  
end  
!read\_attribute(:billed)  
end

The above code does not work in case I do something like:

object = Model.find(:first)  
object.billed = true  
object.save

because in my before\_update filter I am reading the billed column,  
that now has ‘true’, so the validation returns false and the record is  
not saved.

The purpose of this validation is to prevent a record that has been  
marked billed to be changed. But how then am I going to update billed  
to true?

I’ve tried using self.service.update\_attribute(‘billed’, true), doest  
work. update\_attribute\_with\_validation\_skipping returns unknown method  
error.

Thanks!

---

<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:** [December 15, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/reading-model-data-before-update/123240/2 "2007-12-15T12:42:04Z")

</div>

On Dec 15, 2007 1:35 AM, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> errors.add_to_base('This service has already been billed for')
> 
> ```
> 
> because in my before\_update filter I am reading the billed column,
> 
> Thanks!
> 
> >

Just find the old one:

old\_model = Model.find id

and then verify some stuff on the old model.

Pat

---

<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:** [December 15, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/reading-model-data-before-update/123240/3 "2007-12-15T22:52:38Z")

</div>

I knew it was easy lol

Thanks a lot.
