You can track changes to a ActiveRecord model with
ActiveRecord::Dirty#changed?. Yet there is one caveat though, the tracking is lost after the model is saved. In this tutorial I’ll show how to track the changes to a record even the record has been saved, this comes handy when being used with ActiveRecord
Imagine that we have a
Product model with
name, we make change to
1 2 3 4
By using ActiveRecord::Dirty, we could tell if the model or attribute
name is changed.
If we save our model
1 2 3
false value indicates that ActiveRecord::Dirty is not persisent after the model is saved. What if now you are being asked to add in a code to notify the inventory system if product name is changed?
Well, luckily Rails provides us
previous_changes method which entails what
changes has made previously.
Let’s see how we write this:
1 2 3 4 5 6 7 8 9 10
after_update callback to check if any changes previously
name. If so, we invoke
Alternatively, there is another way to write this code. That is to use
around callback. Here is details:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
around callback is introduced in Rails 3. This callback consists of 3 parts:
- The part before
yieldis codes prior to the action is called
yieldis action that gets called
- The part after
yieldis codes after the action is called
In our example above, we store the changing state of attribute
named_change variable. The
yield executes the
update action. The changing state then can be referenced afterward and will send out notification if name is truly changed.
previous_changes is convenient to tell if changes are made to attributes.
I hope that you should take advantage of it more. See you in the next tutorial.