# Proper way for deploying many applications sharing a common user database

**URL:** <https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416>\
**Category:** Rails\
**Created:** [December 28, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416 "2007-12-28T14:51:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ersin\_Er](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e36b37/32.png) [@Ersin\_Er](https://www.ruby-forum.com/u/Ersin_Er)\
**Post date:** [December 28, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416/1 "2007-12-28T14:51:12Z")

</div>

Hi,

I know that it’s possible to use multiple databases with Rails.  
However most of the examples I have seen were for high availability. I  
would like to learn if anyone has any experience with a shared  
database among many rails applications. Such a case may make sense for  
shared user information. Any ideas?

Thanks.

–  
Ersin Er

---

<div class="post-metadata">

**Author:** ![Ersin\_Er](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e36b37/32.png) [@Ersin\_Er](https://www.ruby-forum.com/u/Ersin_Er)\
**Post date:** [December 28, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416/2 "2007-12-28T15:45:47Z")

</div>

We use one database for a shared application which includes an ecommerce  
site, the associated admin site, and the orders site.

All you have to do is make sure your database.yml files match, and be  
sure to copy your models into each site. It works well, but…

It really violates the DRY principle. I’m not sure why our sites were  
set up like this, but the more I use it the more irritated I get when I  
run into an error because I didn’t copy a model from site to site.

If you can figure out how to get past this, or it doesn’t apply, then  
using one database shared between sites isn’t too big of a deal.

---

<div class="post-metadata">

**Author:** ![Ersin\_Er](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e36b37/32.png) [@Ersin\_Er](https://www.ruby-forum.com/u/Ersin_Er)\
**Post date:** [December 28, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416/3 "2007-12-28T15:54:04Z")

</div>

You could put your models and other shared parts of all these  
applications in a plugin in a seperate repository and include that  
plugin in your applications through svn:externals. That way, whenever  
you deploy your apps, svn pulls the most recent versions of your  
shared files.

---

<div class="post-metadata">

**Author:** ![Ersin\_Er](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e36b37/32.png) [@Ersin\_Er](https://www.ruby-forum.com/u/Ersin_Er)\
**Post date:** [December 28, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416/4 "2007-12-28T16:41:12Z")

</div>

On Dec 28, 2007, at 7:50 AM, Ersin Er wrote:

> Hi,
> 
> I know that it’s possible to use multiple databases with Rails.  
> However most of the examples I have seen were for high availability. I  
> would like to learn if anyone has any experience with a shared  
> database among many rails applications. Such a case may make sense for  
> shared user information. Any ideas?

I have not done anything like this yet (am planning on it for a  
future project), but I did just come across this in the api docs:

Connections are usually created through  
ActiveRecord::Base.establish\_connection and retrieved by  
ActiveRecord::Base.connection. All classes inheriting from  
ActiveRecord::Base will use this connection. But you can also set a  
class-specific connection. For example, if Course is an  
ActiveRecord::Base, but resides in a different database, you can just  
say Course.establish\_connection and Course _and all its subclasses_  
will use this connection instead.  
This feature is implemented by keeping a connection pool in  
ActiveRecord::Base that is a Hash indexed by the class. If a  
connection is requested, the retrieve\_connection method will go up  
the class-hierarchy until a connection is found in the connection pool.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![Ersin\_Er](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e36b37/32.png) [@Ersin\_Er](https://www.ruby-forum.com/u/Ersin_Er)\
**Post date:** [December 29, 2007, 12:47am UTC](https://www.ruby-forum.com/t/proper-way-for-deploying-many-applications-sharing-a-common-user-database/124416/5 "2007-12-29T00:47:28Z")

</div>

What about creating a rails application to act as an authentication  
service? Then use ActiveResource to authenticate your users from each  
of your applications in place of ActiveRecord? I think that’s the  
approach I would take. Another option would be to use an LDAP server  
for storing and authenticating users.

In other words use a Single Sign On (SSO) approach to authentication.  
Another really good option would be to implement OpenID and forego the  
whole username/password combination all together. I for one will be  
very happy once the world really catches onto this. To have one single  
source of identity for every site I use would be fantastic.

Just some ideas to think about. Either of these methods would be DRY  
and very flexible.
