Topic: subdomains as account keys [best practices]

on this page: http://wiki.rubyonrails.com/rails/pages … ccountKeys

the example for the Account location Plugin written by DHH shows the following:


#application.rb
    include AccountLocation
    before_filter :find_account

    protected
      def find_account
        @account = Account.find_by_username(account_subdomain)
      end


This makes a call to the database for every single request.

Is there anything wrong with doing it like this?

#application.rb
    include AccountLocation
    before_filter :find_account

    protected
      def find_account
        session[:account] ||= Account.find_by_username(account_subdomain)
      end


That way there is only one call to the database to access the account information and create the session.  Am i missing something? This seemed way too obvious.

Re: subdomains as account keys [best practices]

Aside from storing entire Models in a session, not that I can see.

http://railscasts.com/episodes/13

Re: subdomains as account keys [best practices]

As Ryan's screencast shows your far better saving the account_id rather than the whole model. Doing it your way will make it less easy to update and manage your model.