# Sharing a Model

**URL:** <https://www.ruby-forum.com/t/sharing-a-model/52918>\
**Category:** Rails\
**Created:** [January 20, 2006, 10:53am UTC](https://www.ruby-forum.com/t/sharing-a-model/52918 "2006-01-20T10:53:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 10:53am UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/1 "2006-01-20T10:53:34Z")

</div>

I have two rails applications that modify/view the same data. Is there  
a DRY way of sharing a model (not just database) between two separate  
applications?

---

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/2 "2006-01-20T12:08:59Z")

</div>

You can use svn:externals if you’re using subversion.

One thing I’d like to know from people with more experience is how to  
package up unit tests when using svn:externals. I have a couple  
models that I store in a lib dir external to all my projects, but I’m  
not sure how to handle unit tests.

Pat

---

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/3 "2006-01-20T12:13:43Z")

</div>

I would be pretty interesting if you did some drb experiments on this  
topic…

could be the, dare i say, ejb of rails…

M

Mike Douglas wrote:

> I have two rails applications that modify/view the same data. Is there  
> a DRY way of sharing a model (not just database) between two separate  
> applications?

---

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/4 "2006-01-20T18:59:58Z")

</div>

On 20/01/06, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can use svn:externals if you’re using subversion.
> 
> One thing I’d like to know from people with more experience is how to  
> package up unit tests when using svn:externals. I have a couple  
> models that I store in a lib dir external to all my projects, but I’m  
> not sure how to handle unit tests.

Have you tried making an engine out of common models? I think I have  
seen a  
patch allowing to unit-test engines so this should be a perfect  
solution.

---

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/5 "2006-01-20T19:09:03Z")

</div>

No, save us, no!

---

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/6 "2006-01-20T19:24:09Z")

</div>

Engines provide some support for testing out of the box, although it  
could be improved, with suggestions.

- james

---

<div class="post-metadata">

**Author:** ![Mike\_Douglas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Mike\_Douglas](https://www.ruby-forum.com/u/Mike_Douglas)\
**Post date:** [January 20, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/sharing-a-model/52918/7 "2006-01-20T18:03:35Z")

</div>

Mike Douglas wrote:

> I have two rails applications that modify/view the same data. Is there  
> a DRY way of sharing a model (not just database) between two separate  
> applications?

Ran into this not long ago. We have one main database, and at present  
are running three main apps off of it. Here is the setup on our server:

www

- main\_app
- side\_app
- side\_app\_2

So, both side apps need to share the models from the main app. So what I  
did was remove any models from the side\_app that needed to be shared  
from the main app. Then, in each side app’s environment.rb I added this:

if ENV[‘RAILS\_ENV’] == ‘production’  
config.load\_paths += %W( /home/user/www/main\_app/app/models )  
else  
config.load\_paths += %W( /users/user/sites/main\_app/app/models )  
end

This needs to go in the Rails Initiator block in environment.rb (look  
for the commented config.load\_paths line)

The first part of the conditional makes sure the production box loads  
the models from the main app, the second part makes sure it works on my  
development machine.

A few caveats: You are only sharing the models, so tests aren’t along  
for the ride. I am assuming you can do the same thing and add the tests  
folder to the load path as well. Also, any changes to the main app’s  
models requires a restart of all dependent apps, obviously.
