Created
August 14, 2010 00:26
-
-
Save insane-dreamer/523774 to your computer and use it in GitHub Desktop.
access current_user in an Observer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
before_filter :set_current_user | |
private | |
def set_current_user | |
UserActionObserver.current_user = current_user | |
end | |
end | |
class UserActionObserver < ActiveRecord::Observer | |
# assuming these all have created_by_id and updated_by_id | |
observe Product, Thing, OtherThing | |
cattr_accessor :current_user | |
def before_create(model) | |
model.created_by = @@current_user | |
end | |
def before_update(model) | |
model.updated_by = @@current_user | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment