Skip to content

Instantly share code, notes, and snippets.

@insane-dreamer
Created August 14, 2010 00:26
Show Gist options
  • Save insane-dreamer/523774 to your computer and use it in GitHub Desktop.
Save insane-dreamer/523774 to your computer and use it in GitHub Desktop.
access current_user in an Observer
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