Track changes when updating a record
How do I check if the data is changed when I edit a record?
So before update
game.player=1
after update / edit
game.player=2
for example
开发者_Python百科how to track changes (check if changed) and do something in ruby on rails, when the data is changed.
Have a look at ActiveModel::Dirty
You can check if a model has been changed by doing:
game.changed?
Or an individual field like:
game.player_changed?
Both return a boolean value.
All changes are also kept in a hash. This comes in handy if you want to use the values.
game.changes #=> {:player => [1,2]}
精彩评论