Database depending on URL

Hi All,

I’m brand new to ROR so sorry for the noob question. Been searching the
net
for this one but haven’t been able to figure it out.

My ROR application talks to an existing database. Yup I figured out how
to
make ActiveRecord work with tables that already exist and don’t play to
the
naming convention of rails, all good there.
What makes my existing situation tricky is that my server has a number
of
copies of the same database but I need to access one of those databases
based on the URL

So:
http://database1.myserver.au/mytable => lists records for mytable in
database1
http://database2.myserver.au/mytable => lists records for mytable in
database2

There are two parts to this puzzle.

  1. how do I access and parse the hostname to obtain the database name ?
  2. how do I then use this name in database.yml ?

I’m using Rails 4 btw.

Thanks for any pointers!

You can search about multitenancy in rails.
Some useful links:

2014-05-12 20:40 GMT-03:00 Bastiaan O. [email protected]:

On Monday, 12 May 2014 19:40:10 UTC-4, Bastiaan O. wrote:

copies of the same database but I need to access one of those databases
2) how do I then use this name in database.yml ?

If there’s really a fixed set of DBs to choose from, the simplest
alternative would be to provision each subdomain as a separate copy of
the
application with different environment variables. For instance, in an
Apache / Passenger stack you could use SetEnv in each vhost’s
configuration
to pass a different DATABASE_URL.

–Matt J.

On Mon, May 12, 2014 at 4:40 PM, Bastiaan O. [email protected] wrote:

http://database1.myserver.au/mytable => lists records for mytable in
database1
http://database2.myserver.au/mytable => lists records for mytable in
database2

There are two parts to this puzzle.

  1. how do I access and parse the hostname to obtain the database name ?

See the API doc for ActionController::Base

  1. how do I then use this name in database.yml ?

you don’t, but see ActiveRecord::Base for accessing a model in
a different DB.

HTH,

Hassan S. ------------------------ [email protected]

twitter: @hassan

Hassan, I’ll be sure to check out those leads. It would be viable to
have
to add new entries into database.yml whenever we put a new site online
and
switching between them but it would be preferably to create the pools
for a
specific database when it is first used (with maybe a way to clean up)

Matt, with a separate copy of the application, do you mean duplicating
my
rails app for each database/subdomain combo I have or would just having
different vhosts setup? I can imagine once my rails app loads it would
create the session pool for that database requiring the duplication of
the
app but I would rather avoid that.

Thanks for the feedback guys! Lots to learn:)

I’m going to check out that book, thanks Carlos!

On Tuesday, 13 May 2014 19:38:51 UTC-4, Bastiaan O. wrote:

app but I would rather avoid that.

You should be able to point multiple vhosts at the same source files.
You
will need to specify unique log file locations, though.

–Matt J.