Home > other >  rail: how to know which action in after_xxx in model
rail: how to know which action in after_xxx in model

Time:05-20

I have this code in my model

after_create :publish_properties
after_update :publish_properties
before_destroy :publish_properties

Inside publish_properties, how can i know which was the action (update, create or delete) ?

CodePudding user response:

You could use the callback block syntax (https://guides.rubyonrails.org/active_record_callbacks.html#callback-registration) to explicitly call your callback with the action type like this

after_create { publish_properties :create }
...

def publish_properties(action_type)
 ...
end

CodePudding user response:

I think you want transaction_include_any_action. You can do this by using the below code in your function to get the action name

Rails 4

self.transaction_include_any_action?([:<action_name>])

Rails < 4

self.transaction_include_action?([:<action_name>])

In this action names can be :create, :destroy, :commit, etc.

  • Related