mardi 5 avril 2016

Passing arguments to after(:create) hook in rails. Is this why callbacks aren't great?

This is my code in a model:

class User
...
  after_create :log_creation!
...

  def log_creation!
    Event.create!(
      :event_type => Event::EventType::USER_ACCOUNT_STARTED,
      :date => Time.now.utc,
      :eventable => self,
      :description => "Automatically populated"
    )
  end
end

I need to add something to the create method that deals with the request object which is only available in the controller. What might be the easiest way to do this?

Is this perhaps why people don't like these ActiveRecord hooks? It creates coupling with anything to do with User creation and as a result, when it comes down to change this couple behavior... I need to track down all the places where Users are saved which is annoying in large legacy code. Is this why?

Aucun commentaire:

Enregistrer un commentaire