0

I am trying to determine if an attribute has changed before an update action is applied.

For example the a user clicks a checkbox which then submits a value of true of false to the column foo.

What is the best way to make this comparison before the form has been submitted?

Xavier
  • 831
  • 3
  • 9
  • 22
  • http://stackoverflow.com/questions/5051135/rails-3-check-if-attribute-changed – AbM Jul 06 '15 at 18:47
  • @AbM but doesn't that fire after the record has been updated? – Xavier Jul 06 '15 at 18:47
  • No it fires before. You can use in it a `before_validation`. For instance, check how [Geocoder](https://github.com/alexreisner/geocoder#avoiding-unnecessary-api-requests) does it to prevent unnecessary requests – AbM Jul 06 '15 at 18:50
  • Note that this logic takes place on the server side, so the form has to be submitted to the server. If you want to do the comparison on the client-side, you have to use javascript. A sample implementation for [Angular](http://stackoverflow.com/questions/18073230/in-angularjs-whats-the-difference-between-ng-pristine-and-ng-dirty) – AbM Jul 06 '15 at 18:56

1 Answers1

1
before_update :check_changed_attributes

def check_changed_attributes
  perform_this_action if @foo.bar_changed?
end
Collin Graves
  • 2,207
  • 15
  • 11