Skip to content

Instantly share code, notes, and snippets.

@emmasteimann
Forked from insane-dreamer/gist:523774
Created August 17, 2012 21:18
Show Gist options
  • Save emmasteimann/3382794 to your computer and use it in GitHub Desktop.
Save emmasteimann/3382794 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