ruby on rails - How to detect attribute changes from model? -
I want to create a callback function which is executed after a model is saved.
I have this model, claiming that there is a feature 'position' which changes based on the claim's status, possible values are pending, support, approved, rejected
The default value of 'state' in the database is 'pending'.
After the model is designed for the first time or after updating from one state to another, I want to do some work, depending on which state it varies.
My idea is a function in the model:
after_save: check_state def check_state # If the status has been changed to zero (made) then # this position Pending approval has been changed for the display explorer
My question is how do I check for the previous value before the change in the model?
You should look at the shawl module: you should be able to do the following on your claim model:
claim.status_changed? # Returns correct if the 'status' attribute has changed the claim. Status_was # returns the previous value of the 'status' attribute claim.status_change # = & gt; ['Old value', 'new value'] gives old and # new values to the claim of 'status' attribute. Name = 'Bob' claims. Change # = & gt; ["Name"] Claim. Change # = & gt; {"Name" = & gt; ["Bill", "Bob"]}
Oh! The joys of the train!
Comments
Post a Comment