map.connect / url re-writing

Ben Winekur wrote:

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])


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. => needs to find my information => needs to look up someone else

So it depends on the intended purpose.

Ben Winekur wrote:

Is a single extra query worth worrying over? (I don't honestly know myself.)

If you get a performance problem optimize then. Don't worry about it until you actually have a problem.



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 :slight_smile:

Good luck!