Possible to access the session from an Observer?


#1

Hi,

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:

http://philip.greenspun.com/doc/audit

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

last_modified date not null,
last_modifying_user not null references users,

…now 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 >>
https://activerbac.turingstudio.com/trac).

I can’t think of any other ways for my Observer to get the relevant
data.

Does anyone have any idea?

Thanks in advance,

Michael

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
of):

class NaturalPersonObserver < ActiveRecord::Observer
def before_update(person)
audit = NaturalPersonAudit.new(person)
audit.save
end

def before_save(person)
person.last_modifying_user = user
person.last_modified = Date.now
end

def before_destroy(person)
audit = NaturalPersonAudit.new(person)
audit.save
audit = NaturalPersonAudit.new(person)
audit.delete_p = true
audit.last_modifying_user = user
audit.last_modified = Date.now
audit.save
end
end