user753676
user753676

Reputation:

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

how to track changes (check if changed) and do something in ruby on rails, when the data is changed.

Upvotes: 1

Views: 3528

Answers (2)

Tom Maeckelberghe
Tom Maeckelberghe

Reputation: 1999

All changes are also kept in a hash. This comes in handy if you want to use the values.

game.changes #=> {:player => [1,2]}

Upvotes: 3

Gazler
Gazler

Reputation: 84140

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.

Upvotes: 4

Related Questions