Model after_* methods

Hello,

I have three models setup as such…

User
has_many :requests

Request
has_many :changes

Changes
belongs_to :request
belongs_to :who, :class_name => “User”, :foreign_key => “user_id”

A user logs into the system and the id is stored in the session…
Whenever a user creates or edits a request I want an entry created in
the changes table. Basically spelling the who, what, and when…

I have everything working how I want except the “who” part… I am
unsure of an elegant approach. I am using the after_* methods, but the
session is inaccessible to the model (as it should be)… How can I
(the rails way) log the user_id for the change?

2008/8/4 predhme [email protected]:

unsure of an elegant approach. I am using the after_* methods, but the
session is inaccessible to the model (as it should be)… How can I
(the rails way) log the user_id for the change?

I’ve solved exactly that by adding a User.current_user attribute
that is set in an “authorize” before_filter that is run for every
request. If you want that to be threadsafe, you can store the
current user in Thread.current[:_my_app_current_user] (but I’d
define User.current_user as a facade anyway).

Stefan

Thanks much, that worked. I appreciate it.

On Aug 4, 6:00 pm, “Stefan L.” [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs