Search code examples
ruby-on-railsrubymodelaction

Identify which action is being called inside a model. Ruby on Rails


How do I come to know whether a create or any other action has been called from inside a model. Basically I am doing database logging and want to track whether a create or other actions are being performed.

For doing the logging part I am using the concept of ActiveRecord::Observer. But there I am not able to find out whether the user is creating or doing something else. So please tell me some way that rails provides us to identify the action inside the model.

thanks in advance.


Solution

  • You can use a before_save to trigger an event when the model is saved. To determine if it is a save or a create you have this method new_record? which will tell you if it's a new instance or not. You can also know which attributes have changed