test is a specific field is modified

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 :slight_smile: I would use the middle solution if I had lots of stuff to do when a record was modified.


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.