Skip to content

Instantly share code, notes, and snippets.

@emarthinsen
Created January 3, 2013 05:42
Show Gist options
  • Save emarthinsen/4441100 to your computer and use it in GitHub Desktop.
Save emarthinsen/4441100 to your computer and use it in GitHub Desktop.
Trying to get some custom AR callbacks to trigger corresponding handlers in an observer
class DashboardObserver < ActiveRecord::Observer
observe :user
def after_activate(user)
DashboardService.update_metrics
end
end
class User < ActiveRecord::Base
include User::Authentication
define_model_callbacks :upgrade, :downgrade, :cancel, :activate, :deactivate
#large swaths of code
def activate!
run_callbacks :activate do
self.update_attribute(:active, true)
end
end
end
@emarthinsen
Copy link
Author

Note, I'm trying to create a custom callback in User. When a user is activated, I want the 'activate' callback to fire and to be handled in my DashboardObserver. As the code stands, the observer never sees the callback get raised.

Note, I'm using the define_model_callbacks method above. I get the same results if I use define_model.

And, yes, I've correctly registered the observer in application.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment