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.

Hi everyone.
I'm writing the admin area of a website, which has several users. The id
of
the current user is stored in the session. There are several models that
the
user can edited (article, link, customer etc), and whenever a user edits
a
model I would like to record this fact in a textfield of the model, so
that
lists of the various models can display who last edited them. I wrote a
module to include into each model that wants this behaviour, but then I
realised I can't get at the user_id from the model, and that I would
have to
go edit all my controllers, passing in the user_id to each different
model
wherever an instance is created. This is obviously hairy duplication,
and
I'm sure there is a clever way to factor it out, I just can't see where.
Cheers.
Robie

Have a look at the way Bruce Perens has done it in ModelSecurity
(http://perens.com/FreeSoftware/ModelSecurity/Tutorial.html).
Basically he creates a class attribute on User called 'current', and
uses a before_filter to pull the current user out of the session and
set it in the User class.
It's quite a neat solution, and one that I'm adopting in my own code.
Several of my tables have a 'created_by_id' that automatically gets
set to the id of the current user on creation, and that can be done
entirely in the model.
Pete Yandell