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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B02a6400d9991d347ad2a11f1002c88d?d=identicon&s=25 Michael Henry (aviast)
on 2006-12-25 10:15
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
This topic is locked and can not be replied to.