← Back to Upcase

Avoiding after_save callback that relies on activerecord.changed?

(Nicolo) #1

I’m trying to avoid to using an after_save active record callback that relies on the attributes being changed.

Basically, I want to create a class that takes a active record object that executes some code if certain attributes have changed on last save.

The problem is that all active record change tracking is removed after call .save on the object. Any suggestions for how to accomplish this without using an after save callback?

0 Likes

(Chad Pytel) #2

@Nicolo can you provide more concrete examples of what the attributes are, what they are changing to, and why it needs to be triggered off the attribute changing?

0 Likes

(Nicolo) #3

Yes I import stats for players and save them to my database. I want to update the player’s statistics on the client via Pusher. I only want to send a request to pusher if the stats are actually changed for that player.

0 Likes

(Calle Erlandsson) #4

@Nicolo I think ActiveModel::Dirty#previous_changes might be what you’re looking for. It returns a Hash of attributes that were changed before the model was saved.

0 Likes

(Nicolo) #5

@calleerlandsson I think that will do it. Thanks for finding that. I can’t believe I missed that.

0 Likes

(Calle Erlandsson) #6

No problem @Nicolo.

0 Likes