Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

UserStamp: Automatic created_by / updated_by for Rails models

By Peter Cooper / June 13, 2006

Delynn Berry has developed a useful plugin called UserStamp. Whereas Rails has built in support for automatic columns such as created_at and updated_at, it doesn't (quite rightly) support concepts such as created_by, as these belong in the application domain. It's a commonly required feature though, and Delynn has done a great job of packaging it up.

This is also worth looking at if you want to get an idea of how to put a similar plugin together. Most Rails applications have similar requirements and packaging them up as plugins makes it a lot easier to manage.


  1. Scott says:

    The one potential issue with UserStamp -- not today but perhaps at some point in the future -- is support for handling multiple requests via threads in Rails. The fact that you set the "current user" using a class-level attribute gives me pause. What I ended-up doing myself was to add versions of all of the save-related methods with a user parameter ("save_with_user" for example) and passed the user in explicitly so that I wasn't dependent on cross-thread-globals, which is pretty much exactly what UserStamp does under the covers. This all probably sounds silly since it's not an issue today, but I'm crazy like that. - Scott

  2. Peter Cooper says:

    Thanks for the comment, Scott.

    Is it going to be an issue in the future though? I'm probably not as au-fait with the latest things that'll be coming in Rails as you are, possibly, but surely it's necessary to always run the before_filters before any request, threaded or not?

    For example, most apps I know of (and which I develop myself) have some sort of 'current_user' attribute to use all over, and while I don't tend to tack it onto the User class directly, it has much the same function.

  3. Tarmo says:

    The problem of threading can easily be solved by using thread local storage to store the attributes.

    Just use Thread.current[:whatever] and it is guaranteed to be visible only to the thread that is handling the request.

Other Posts to Enjoy

Twitter Mentions