# Having two rails apps share the same models

**URL:** <https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644>\
**Category:** Rails\
**Created:** [May 5, 2008, 3:43am UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644 "2008-05-05T03:43:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 3:43am UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/1 "2008-05-05T03:43:18Z")

</div>

Hi,

Say I have a public facing website, and a separate admin-only website  
(that needs to be separate). They do need to share some, if not all,  
of the models and libraries.

The simplest thing I can think of is to have the models and libraries  
somewhere separate, maybe in their own git repository as a submodule,  
then on deploy of each application, copy them into the app/lib  
directories.

That seem reasonable?

Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 3:46am UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/2 "2008-05-05T03:46:14Z")

</div>

Joe Van D. wrote:

> Hi,
> 
> Say I have a public facing website, and a separate admin-only website  
> (that needs to be separate). They do need to share some, if not all,  
> of the models and libraries.
> 
> The simplest thing I can think of is to have the models and libraries  
> somewhere separate, maybe in their own git repository as a submodule,  
> then on deploy of each application, copy them into the app/lib  
> directories.
> 
> That seem reasonable?
> 
> Joe

/project  
/public  
/admin  
/common

you’ll have to deploy them in this structure

but then add to both of your config/environment.rb

config.load\_paths += %W( #{RAILS\_ROOT}/…/common/app/models )

should work.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 3:52am UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/3 "2008-05-05T03:52:04Z")

</div>

On May 4, 6:46 pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> > directories.  
> > you’ll have to deploy them in this structure
> 
> but then add to both of your config/environment.rb
> 
> config.load\_paths += %W( #{RAILS\_ROOT}/…/common/app/models )
> 
> should work.

Makes sense, I’ll give it a try. Anyone done anything like this? The  
main reasons for this are:

1. Security. Easier to protect admin section of site if it’s not  
accessible via the public network.
2. Upgradability. We can upgrade the admin part of the site without  
affecting anything on the public one.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 11:54am UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/4 "2008-05-05T11:54:47Z")

</div>

Hi Joe,

why the application _needs_ to be “physically” separated into two  
standalone Rails apps? Security concerns, separate developers, or …?

You probably set yourself for couple of troubles this way. If you  
really need them to be separate, I’d suggest to put shared logic into  
a standard Rails plugin.

Karel

–  
www.karmi.cz

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 5:47am UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/5 "2008-05-05T05:47:15Z")

</div>

I have the following two URLs which are actually the same APP:

[http://www.flute-fidelity.com.au/](http://www.flute-fidelity.com.au/)  
[http://www.billyhydewoodwind.com.au/](http://www.billyhydewoodwind.com.au/)

I find it’s quite easy to get rails to switch functionality based on  
the URL that it gets driven from. Why not simply have two virtual  
hosts pointing to the same web app, and put some webserver rules on  
one for being local only, and on the other for being public only.

Then, the rails app can know whether or not to honour its admin side  
or not. Makes it easier for development than messing around with  
multiple apps accessing the same codebase (i’ve done that before, too,  
and it’s a bit on a nightmare).

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 coming soon!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/6 "2008-05-05T16:25:11Z")

</div>

> Have you looked into sharing models between your apps using  
> ActiveResource?[http://railscasts.com/episodes;archive](http://railscasts.com/episodes;archive) (episodes 94 and 95)  
> Assuming that you absolutely must separate into two applications, this  
> would be my recommendation. The admin side of things should be the  
> lower-traffic side so I’d suggest using ActiveResource there to access  
> the objects in the public app.

> why the application _needs_ to be “physically” separated into two  
> standalone Rails apps? Security concerns, separate developers, or …?  
> Karel does ask a good question. It’s also reasonable to use the  
> map.namespace options to create an admin area in the site. It could  
> use whatever authentication/authorization scheme you want so you could  
> secure it in a different way than your public site. Certainly this  
> would be easier than supporting separate apps.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [May 5, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/having-two-rails-apps-share-the-same-models/136644/7 "2008-05-05T14:16:03Z")

</div>

I have a similar situation where I have one application that acts as a  
portal to 3 other applications.  
The portal application’s database is VERY bare-bones and holds the  
minimum amount of information  
for my shared models. (Like users)

We turned our models into a plugin and had all the apps bring in the  
plugin as an SVN external.

class User \< ActiveRecord::Base  
unloadadable self

def self.table\_name  
“\*YOURAPPSNAME\_” + ENV[“RAILS\_ENV”] + “.” + “users”  
end  
…  
end

So far this is serving us well - there are a few gotchas (some  
expected, some not expected)…  
All the DBs need to be on the same machine using this approach…even  
though they are different databases.

Also - it makes any changes to that plugin cascade across all your  
applications - so you need to account  
for the way your applications interface with those models.

One major boost we’re seeing out of this approach is the ability to  
decorate models in each one of the application.  
So a user may get new methods and associations in application1 - we  
can easily make a UserDecorator module in our  
/lib folder to give the user these methods and associations.

Have you looked into sharing models between your apps using  
ActiveResource?  
[http://railscasts.com/episodes;archive](http://railscasts.com/episodes;archive) (episodes 94 and 95)
