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: http://railscasts.com/episodes/388-multitenancy-with-scopes https://leanpub.com/multi-tenancy-rails

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

2) 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,

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 Jones

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

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

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.

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

–Matt Jones