# Model after\_\* methods

**URL:** <https://www.ruby-forum.com/t/model-after-methods/144976>\
**Category:** Rails\
**Created:** [August 4, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/model-after-methods/144976 "2008-08-04T22:12:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [August 4, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/model-after-methods/144976/1 "2008-08-04T22:12:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [August 5, 2008, 12:00am UTC](https://www.ruby-forum.com/t/model-after-methods/144976/2 "2008-08-05T00:00:54Z")

</div>

2008/8/4 predhme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![predhme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@predhme](https://www.ruby-forum.com/u/predhme)\
**Post date:** [August 22, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/model-after-methods/144976/3 "2008-08-22T13:18:52Z")

</div>

Thanks much, that worked. I appreciate it.

On Aug 4, 6:00 pm, “Stefan L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
