Ákos Vandra-Meyer
Ákos Vandra-Meyer

Reputation: 2175

Get version of model before it was saved?

I have a Sidekiq Job that syncs one of my models to an external source. This is a - let's say expensive - operation, that I would like to run only when needed. Reading the current state of the external model is also expensive, and does not return the exact format we need to supply when saving.

I have a method that translates ActiveRecord model -> External model.

Is there a good way to decide whether the External model generated from the old and the new versions of the ActiveRecord model are different?

I am currently triggering the job in the after_saved callback. I have access to saved_changes, but that only returns a bunch of attributes that have changed, and not the actual old version of the ActiveRecord model. That information can of course be used to rebuild the old model, but that won't work, or would take significant effort to make it work in more complicated cases (nested_attributes, relations, etc). Is there a way to actually get it back?

Sure, one way would be to save the external model into the database in some way, and compare with that, but that I would like to avoid that as well.

Upvotes: 0

Views: 54

Answers (0)

Related Questions