Links
Userstamp
Categories
Userstamp
The Userstamp Plugin extends ActiveRecord::Base to add automatic updating of createdby and updatedby attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created(at/on) and updated(at/on) attributes.
The module requires that your application’s user object (User by default) contains an accessor called currentuser be set with an instance of the currently logged in user (typically using a beforefilter. This module can also be turned off on a case by case basis by setting the record_userstamps attribute of your ActiveRecord object to false.
Vitals
| Home | http://delynnberry.com/projects/userstamp |
|---|---|
| Repository | svn://delynnberry.com/code/plugins/userstamp/trunk |
| License | OpenSource |
| Tags |
|
| Rating | (12 votes) |
| Owner | Delynn Berry |
| Created | 14 April 2006 |
Comments
-
I had difficulties with userstamp plugin in combination with a custom before_filter. After thinking about other ways to achieve what the plugin does, I found a very simple solution:
In your environment.rb put
set createdby and updatedby on create and update
class ActiveRecord::Base before_update UserStamp.new before_create UserStamp.new end
and then add a file UserStamp.rb into app/models, containing
class UserStamp def before_update(model) if nil != User.currentuser && model.respondto?(:updated_by) model.updatedby = User.currentuser end end
def before_create(model) if nil != User.currentuser && model.respondto?(:created_by) model.createdby = User.currentuser end end end
Same prerequisites as for the plugin: you need to have User.currentuser set to a user, have the belongsto set in your models and should have fields called createdby and updatedby.
That's it - and thanks to Delynn for providing the original idea.
-
The one below worked well for me
The original plugin gave me a stack error so I removed it.
-
The solution below does not work for me.
-
make sure to define the foreign key in your user.rb
ex:
hasmany :posts, :foreignkey => "created_by" -
I am trying to use this plugin but the repository is asking for a username and password. Is there an anonymous login or is this just an error?
-
Made some changes to work with Rails 2.0.2 at http://github.com/ctran/annotate_models
-
When I try to install, it does not seem to work, all i get is one line of feedback:
> ruby script\plugin install git://github.com/delynn/user stamp.git removing: C:/dev/XXX/YYY/vendor/plugins/userstamp/.git
-
I was getting a nasty little error until I modified userstamp.rb too look like this: def setcreateuserstamp return unless usermodel.respondto? :current_user if recorduserstamp && usermodel.current_user self[createdby_attr] = usermodel.currentuser.id if respondto?(created_by_attr) end end
-
FYI, If you are using compatibility mode and you are getting nulls in the createdby and updatedby fields, this may help... I had a problem with this plugin on Rails 2.2.2 where it worked fine on my local machine in dev mode but the createdby and updatedby were null in staging and production. The fix was to change the global configuration of compatibility mode in the plugin itself and set it to default to true. After that, works great.

