User stamping in Rails 3

Published July 21st 2011

If you used the user_stamp gem in Rails 2 to maintain creator_id and updater_id columns in every table, you'll find that it doesn't work in Rails 3. I have a patched version on my Github account that works in production mode in Rails 3, but falls victim to class reloading in development mode. Instead of fixing that by breaking the old API, I'm just gonna make a quick post here on how you can implement the functionality yourself without depending on a gem. It's so simple that it's not really worth an external dependency.

Add a new file to app/models called user_stamper.rb:

class UserStamper < ActionController::Caching::Sweeper
observe Post, Comment, User #replace with your own models

def before_validation(record)
return unless current_user
record.send(:creator_id=, if(record.new_record? && record.respond_to?(:creator_id=))
record.send(:updater_id=, if(record.changed? && record.respond_to?(:updater_id=))


def current_user
controller.send(:current_user) if(controller.respond_to?(:current_user))

You'll also need this line in the ApplicationController:

cache_sweeper :user_stamper

That's all there is to it.