Here is one way to solve you problem. Its slightly different from
what you wanted as far as mapping to the url you want but its another
way to use the subdomain as account key:
The filters added to this controller will be run for all
controllers in the application.
Likewise will all the methods added be available for all controllers.
class ApplicationController < ActionController::Base
before_filter :setup_environment, :except =>
[:new, :create, :signup]
if @account = Account.find_by_company_abbrev
@session[‘account_id’] ||= @account.id
redirect_to :controller => ‘user’, :action => ‘login’
(:account_domain, :account_host, :account_url)
@account.company_abbrev if @account && @account.respond_to?
(:company_abbrev) || “default”
def account_url(account_subdomain = default_account_subdomain,
use_ssl = request.ssl?)
(use_ssl ? “https://” : “http://”) + account_host
def account_host(account_subdomain = default_account_subdomain)
account_host = ""
account_host << account_subdomain + "."
account_host << account_domain
account_domain = ""
account_domain << request.subdomains[1..-1].join(".") + "." if
request.subdomains.size > 1
account_domain << request.domain + request.port_string
With this method I have an Account model that stores the username as
the subdomain key. this before filter gets run on each request and
takes the subdomain from the url and grabs the user that matches it.
Then it sets session[:account_id] to @account.id unless it is already
set. The rest of the methods in the AccountLocation module are just
for creating the correct links that always include the subdomain.
So with this method, instead of redirecting joe.example.com to
example.com/user/joe, the url just always stays joe.example.com
whenever they are logged in. Much simpler then what you want to do I