# Test is a specific field is modified

**URL:** <https://www.ruby-forum.com/t/test-is-a-specific-field-is-modified/90377>\
**Category:** Rails\
**Created:** [March 28, 2007, 10:11am UTC](https://www.ruby-forum.com/t/test-is-a-specific-field-is-modified/90377 "2007-03-28T10:11:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drenovac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@drenovac](https://www.ruby-forum.com/u/drenovac)\
**Post date:** [March 28, 2007, 10:11am UTC](https://www.ruby-forum.com/t/test-is-a-specific-field-is-modified/90377/1 "2007-03-28T10:11:21Z")

</div>

Hello everybody

I’m relatively new to RoR but I find it really interesting. I’ve managed  
to do lots of nice things bt I’m stuck with a specific problem.

I’ve got a table with 5 columns and among it one is a boolean and one is  
a date. There are other fields (text fields…)  
i’ve linked a checkbox to the boolean. I’ve managed to have the date set  
at Time.now whenever I update the record.  
But what I’d really like to do is to update the date field only when the  
boolean is modified (and not when the user modifies some of the other  
fields)

Can anyone help ?

Thanks

Martin

---

<div class="post-metadata">

**Author:** ![drenovac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@drenovac](https://www.ruby-forum.com/u/drenovac)\
**Post date:** [March 28, 2007, 10:31am UTC](https://www.ruby-forum.com/t/test-is-a-specific-field-is-modified/90377/2 "2007-03-28T10:31:33Z")

</div>

On 3/28/07, Martin Da silva [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But what I’d really like to do is to update the date field only when the  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Hey Martin,

There are a couple ways you can do this. The first is to write a  
custom setter method for the boolean value:

class Foo \< ActiveRecord::Base  
def bool\_field=(b)  
self.date\_field = Time.now  
write\_attribute :bool\_field, b  
end  
end

The other way is to use an Observer.

class FooObserver \< ActiveRecord::Observer  
def before\_update(foo)  
old\_foo = Foo.find foo.id  
foo.date\_field = Time.now if foo.bool\_field != old\_foo.bool\_field  
end  
end

or you could put it directly in the Foo class…

class Foo \< ActiveRecord::Base  
before\_update :update\_time

def update\_time  
old\_foo = Foo.find id  
self.date\_field = Time.now if self.bool\_field != old\_foo.bool\_field  
end  
end

Now that I’ve written them all out, I think the last solution would be  
best 🙂 I would use the middle solution if I had lots of stuff to do  
when a record was modified.

Pat

---

<div class="post-metadata">

**Author:** ![drenovac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@drenovac](https://www.ruby-forum.com/u/drenovac)\
**Post date:** [March 28, 2007, 10:33am UTC](https://www.ruby-forum.com/t/test-is-a-specific-field-is-modified/90377/3 "2007-03-28T10:33:33Z")

</div>

On 3/28/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> or you could put it directly in the Foo class…
> 
> class Foo \< ActiveRecord::Base  
> before\_update :update\_time
> 
> def update\_time  
> old\_foo = Foo.find id  
> self.date\_field = Time.now if self.bool\_field != old\_foo.bool\_field  
> end  
> end

I _think_ you would have to add a  
return true

at the end of that method, because otherwise Rails will think that  
some validation failed and it won’t save the record. I’m not 100%  
sure though.

Pat

---

<div class="post-metadata">

**Author:** ![drenovac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@drenovac](https://www.ruby-forum.com/u/drenovac)\
**Post date:** [March 28, 2007, 10:59am UTC](https://www.ruby-forum.com/t/test-is-a-specific-field-is-modified/90377/4 "2007-03-28T10:59:04Z")

</div>

Pat M. wrote:

> On 3/28/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > or you could put it directly in the Foo class…
> > 
> > class Foo \< ActiveRecord::Base  
> > before\_update :update\_time
> > 
> > def update\_time  
> > old\_foo = Foo.find id  
> > self.date\_field = Time.now if self.bool\_field != old\_foo.bool\_field  
> > end  
> > end
> 
> I _think_ you would have to add a  
> return true
> 
> at the end of that method, because otherwise Rails will think that  
> some validation failed and it won’t save the record. I’m not 100%  
> sure though.
> 
> Pat

Well this solution seems to work pretty well. Thanks a lot 🙂
