Forum: Ruby on Rails Database depending on URL

397891a2f33199a6b0a47a6cb6f46080?d=identicon&s=25 Bastiaan Olij (Guest)
on 2014-05-13 04:18
(Received via mailing list)
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!
239f0475374dea328f971332e6adf426?d=identicon&s=25 Carlos Ribeiro (carlosribeiro)
on 2014-05-13 10:36
(Received via mailing list)
You can search about multitenancy in rails.
Some useful links:
http://railscasts.com/episodes/388-multitenancy-with-scopes
https://leanpub.com/multi-tenancy-rails


2014-05-12 20:40 GMT-03:00 Bastiaan Olij <mux213@gmail.com>:
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2014-05-13 14:34
(Received via mailing list)
On Mon, May 12, 2014 at 4:40 PM, Bastiaan Olij <mux213@gmail.com> 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

> 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,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2014-05-13 14:56
(Received via mailing list)
On Monday, 12 May 2014 19:40:10 UTC-4, Bastiaan Olij 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 Jones
397891a2f33199a6b0a47a6cb6f46080?d=identicon&s=25 Bastiaan Olij (Guest)
on 2014-05-14 01:35
(Received via mailing list)
I'm going to check out that book, thanks Carlos!
397891a2f33199a6b0a47a6cb6f46080?d=identicon&s=25 Bastiaan Olij (Guest)
on 2014-05-14 01:40
(Received via mailing list)
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:)
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2014-05-14 18:34
(Received via mailing list)
On Tuesday, 13 May 2014 19:38:51 UTC-4, Bastiaan Olij 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 Jones
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.