I want to automate somewhat the addition of created_by/updated_by fields on my models (i.e. to include username). What's the best/recommended way to do this? Would it be to:
A simple way would be to add records via your user models. Take a users/articles relationship. Article belongs_to User, User has_many Articles.
When you create the article, if you do it like this:
then your articles are automatically linked to the creating user.
The problem with using before filters in the models to do this is that you would some how need to get hold of the user's name - likely to be via the session like this:
user = User.find(params[:user_id])
But including that in the model would be breaking the whole MVC thing.
If you have a small amount of models, it's probably easier to just manually specify the users:
article.created_by = @user.name
article.updated_by = @user.name
As you would only be updating/creating in one place, it's not a huge amount of code.
Any other thoughts on this?