Parse request URL

Hello all.

Due to the mistakes of the company before I joined :wink: I am stuck with a
multiple database setup. I am planning on parsing the REQUEST_URL to
decide which controller is being accessed and basing the
establish_connection on that. Before I go messing around with regexps I
was wondering if rails has a built in function for parsing URLS. I have
googled around a bit and seen net/http but I cannot find an API for it.

Thanks

Jeff

Jeff J. wrote:

Hello all.

Due to the mistakes of the company before I joined :wink: I am stuck with a
multiple database setup. I am planning on parsing the REQUEST_URL to
decide which controller is being accessed and basing the
establish_connection on that. Before I go messing around with regexps I
was wondering if rails has a built in function for parsing URLS. I have
googled around a bit and seen net/http but I cannot find an API for it.

The request URL is chopped up by config/routes.rb. Iā€™d start there.

Alex Y. wrote:

Jeff J. wrote:

Hello all.

Due to the mistakes of the company before I joined :wink: I am stuck with a
multiple database setup. I am planning on parsing the REQUEST_URL to
decide which controller is being accessed and basing the
establish_connection on that. Before I go messing around with regexps I
was wondering if rails has a built in function for parsing URLS. I have
googled around a bit and seen net/http but I cannot find an API for it.

The request URL is chopped up by config/routes.rb. Iā€™d start there.

I found out about require ā€˜URIā€™ and ended up using that.

class ApplicationController < ActionController::Base
before_filter :change_database

def change_database
path = URI.parse(request.env[ā€œREQUEST_URIā€]).path
comp = path.split(ā€™/ā€™)

if comp[1].downcase == 'sheet'
  ActiveRecord::Base.establish_connection(
  :adapter  => "XXX",
  :database => "XXX",
  :host     => "XXX",
  :username => "XXX",
  :password => "XXX"
  )
elsif comp[1].downcase == 'component'
  ActiveRecord::Base.establish_connection(
  :adapter  => "XXX",
  :database => "XXX",
  :host     => "XXX",
  :username => "XXX",
  :password => "XXX"
  )
end

end
end

Cheers

Jeff

Jeff J. wrote:

Alex Y. wrote:

Jeff J. wrote:

Hello all.

Due to the mistakes of the company before I joined :wink: I am stuck with a
multiple database setup. I am planning on parsing the REQUEST_URL to
decide which controller is being accessed and basing the
establish_connection on that. Before I go messing around with regexps I

there are easier ways to determine the controllerā€¦

params[:controller] in a before_filter should do the trickā€¦

Mikkel B. wrote:

there are easier ways to determine the controllerā€¦

params[:controller] in a before_filter should do the trickā€¦

heh,

try self.class in a before filter in ApplicationController, its probably
the best wayā€¦