workflow-activerecord icon indicating copy to clipboard operation
workflow-activerecord copied to clipboard

Triggering after_update on models

Open gregfedorov opened this issue 2 years ago • 1 comments

Previously, back in the Rails 3 days, modifying the workflow state of an object would go through the full "save" action of the record, triggering teh before_save and after_update and all other relevant hooks. Now it seems the state is simply written straight to DB bypassing the hooks. How can I make sure "after_update" gets run on state change?

One way would be of course to do something like: "model.approve! and model.save!", but in this case the "changes" or "saved_changes" objects in the hooks are empty.

gregfedorov avatar Oct 23 '23 10:10 gregfedorov

@gregfedorov you should be able to do this by overriding persist_workflow_state method in your model. I achieved it this way:

def persist_workflow_state(new_value)
  self[self.class.workflow_column] = new_value
  save!
end

workflow-activerecord by default uses update_column method which bypasses any callbacks. https://github.com/geekq/workflow-activerecord/blob/1a4ca5985101495439460979c9773a4c0aae71b2/lib/workflow-activerecord.rb#L20-L23

oskargargas avatar Nov 23 '23 13:11 oskargargas