I realize I can then access :username from params[:username], but I was wondering if it was possible to instantiate a @user variable from params[:username] in one place that's accessible anywhere?
Put the following in your application controller:
before_filter :init_user
def init_user
@user = User.find_by_username(params[:username])
end
Yes you could. However, you would have to make sure you refreshed the session if the user object changed. The before_filter approach seems to be the appropriate method. It also depends on if you’re talking about the same user on each request.
An extra query like that isn’t going to hurt. Databases are designed to be queried. As Eric says below, don’t optimize until you need to. You can cluster your backend databases, you can use model caching, there are lots of options if performance becomes an issue. However, for this… it’s not going to matter. I’d be willing to bet you that there are going to be other areas you’ll need to optimize first