Sarmstrong
Sarmstrong

Reputation: 1

after_update if statement based on attribute values Ruby on Rails

I am trying to send a notification based on a project's status but can't quite get the logic right - my code is running :create_slack_notify_job after any update, however I want :create_slack_notify_job to run only after a specific kind of update (when the project's status was changed to "Active"), not after any attribute update. My after_update is below - any ideas on how to make that run as explained?

after_update :create_slack_notify_job, if: Proc.new{|project|project.status_changed? && project.status == 'Active' }

Upvotes: 0

Views: 548

Answers (1)

Daniel Westendorf
Daniel Westendorf

Reputation: 3475

If you're running Rails 5.1+ you'll want to use the method saved_changes and friends. In particular, you probably want project.saved_change_to_attribute? :status, to: 'Active'

http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-saved_change_to_attribute-3F

https://github.com/rails/rails/pull/19847

Upvotes: 2

Related Questions