Multiple MongoDBs in Rails

157 views
Skip to first unread message

Kapslok

unread,
Mar 5, 2010, 11:34:58 PM3/5/10
to Mongoid
I'm using Devise with subdomains in my project, and would like to work
out how to have a database per subdomain using MongoDB. I'm wondering
if this is possible using Mongoid?

I have setup Mongoid the way it is suggested on the Mongoid.org
website, which creates a connection to the database using the Rails
initializer. I'm wondering if something similar to
establish_connection in ActiveRecord could be used to create the
connection when a request comes in on a subdomain?

Anyone have any thoughts...

Durran Jordan

unread,
Mar 8, 2010, 7:20:08 AM3/8/10
to mon...@googlegroups.com
I dont have an elegant way of doing this at the moment, you'd have to do a little bit of hackery and switch out the master or slaves in the config on the fly at the moment - but I'll have a thought about a good way to handle this and add a story for it... Are you looking to change databases on the fly, or just configure it at the model level?

Vitaliy V. Shopov

unread,
Mar 9, 2010, 9:19:22 PM3/9/10
to mon...@googlegroups.com
I am looking too for to change databases on the fly (for using database per user).

2010/3/8 Durran Jordan <dur...@gmail.com>

Durran Jordan

unread,
Mar 9, 2010, 9:21:26 PM3/9/10
to mon...@googlegroups.com, mon...@googlegroups.com
Yeah I am going to handle this - goes hand in hand with my previous configuration post. I want to make it a little easier to deal with than AR does. I'll post an update as I delve more into it.

Sent from my iPhone

Kapslok

unread,
Mar 14, 2010, 10:35:07 PM3/14/10
to Mongoid
I'm sorry I've taken so long to get back to you.

I do require to change database on the fly and per model.

My app supports users with subdomain accounts. The app will have db
for users, roles, permissions etc. And each account can have multiple
subdomains. Each subdomain will have it's own database.

Ideally it would be nice to do the following:

class ApplicationController < ActionController::Base
@subdomain.database = current_subdomain + "-dev"
end

class User
include Mongoid::Document
Mongoid.database = "global"
end

class Page
include Mongoid::Document
Mongoid.database = @subdomain.database
end

I'm currently using MongoMapper and this is working fine.

I'm keen on trying Mongoid, but without this it's a no go.


On Mar 8, 10:20 pm, Durran Jordan <dur...@gmail.com> wrote:
> I dont have an elegant way of doing this at the moment, you'd have to do a
> little bit of hackery and switch out the master or slaves in the config on
> the fly at the moment - but I'll have a thought about a good way to handle
> this and add a story for it... Are you looking to change databases on the
> fly, or just configure it at the model level?
>

matthewford

unread,
Mar 16, 2010, 9:38:33 AM3/16/10
to Mongoid
We're doing this, the way we've done it is to store the account/roles/
users in mysql and the hierarchical data in mongo, and just do a
conditional search on the account_id.

Adam Fields

unread,
Mar 16, 2010, 4:58:41 PM3/16/10
to Mongoid
I've tried setting Mongoid.config.master to a new connection, and even
though it seems to "stick", it's still reading and writing the
original value from the initializer. Is this cached somewhere, or is
there something else required to change the master db?

On Mar 8, 8:20 am, Durran Jordan <dur...@gmail.com> wrote:
> I dont have an elegant way of doing this at the moment, you'd have to do a
> little bit of hackery and switch out the master or slaves in the config on
> the fly at the moment - but I'll have a thought about a good way to handle
> this and add a story for it... Are you looking to change databases on the
> fly, or just configure it at the model level?
>

Reply all
Reply to author
Forward
0 new messages