Forum: Ruby on Rails Possible to access the session from an Observer?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Michael H. (Guest)
on 2006-12-25 11:15

I'd like to implement an ActiveRecord::Observer to maintain audit tables
for some of my application models. What I intend to do follows this
documentation almost exactly:

I've added the necessary columns to my model table definition:

  last_modified           date not null,
  last_modifying_user     not null references users, I just need to populate those fields. Therein lies the problem.

The ActiveRecord::Observer does not have ready access to the session so
that I can grab session[:rbac_user] which represents my user. (I'm using
ActiveRBAC to handle my access control >>

I can't think of any other ways for my Observer to get the relevant

Does anyone have any idea?

Thanks in advance,


P.S. I'd rather these fields be transparent to the controllers and the
updating of these fields be done at the model layer. There should be no
need to clutter up my controllers with this stuff...

P.P.S. Here's my Observer ("user" is the object I need to get a hold

class NaturalPersonObserver < ActiveRecord::Observer
  def before_update(person)
    audit =

  def before_save(person)
    person.last_modifying_user = user
    person.last_modified =

  def before_destroy(person)
    audit =
    audit =
    audit.delete_p = true
    audit.last_modifying_user = user
    audit.last_modified =
This topic is locked and can not be replied to.