# Best practice for sharing code between rails apps

**URL:** <https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276>\
**Category:** Rails\
**Created:** [July 28, 2006, 3:46am UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276 "2006-07-28T03:46:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 28, 2006, 3:46am UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276/1 "2006-07-28T03:46:52Z")

</div>

Is there a writeup about how to structrure your rails applications so  
that you can share models, gems, libraries, helpers, and controllers  
between them. It would be especially helpful if the discussion  
involved how to structure your codebase in version control and how to  
manage deployement.

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 28, 2006, 4:00am UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276/2 "2006-07-28T04:00:03Z")

</div>

Well, just use subversion for all your apps, then use svn:externals  
to share pieces of them with each other. How’s that for a write-up? 🙂

–  
Benjamin C.  
[http://www.bencurtis.com/](http://www.bencurtis.com/)  
[http://www.tesly.com/](http://www.tesly.com/) – Collaborative test case management  
[http://www.agilewebdevelopment.com/](http://www.agilewebdevelopment.com/) – Resources for the Rails community

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 28, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276/3 "2006-07-28T12:05:27Z")

</div>

> Well, just use subversion for all your apps, then use svn:externals to share  
> pieces of them with each other. How’s that for a write-up? 🙂

How would that work exactly? Let’s say I have a model, or a fragment I  
want to share amongst different ror apps. I presume I check these  
things into their own repos and then “attach” them to my application.  
Can I just specify one file or five files as an external or does it  
have to be an entire directory. If the latter how do I make sure ror  
sees them?

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 28, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276/4 "2006-07-28T15:19:26Z")

</div>

You can only specify directories as externals. So, how you can get  
at them depends on what you want to share. If it’s a set of models,  
you can attach them as a subdirectory of lib and require it from  
there. If it’s a set of views you can attach them to app/views.

These methods are not as convenient as plugins or engines, though.  
In fact, this is the best use-case for engines – sharing common  
functionality among a number of internal applications. Generally  
speaking, you’ll have an easier time of it if you can package up the  
functionality you want to share into a plugin or engine and then  
install the plugin/engine via svn externals.

–  
Benjamin C.  
[http://www.bencurtis.com/](http://www.bencurtis.com/)  
[http://www.tesly.com/](http://www.tesly.com/) – Collaborative test case management  
[http://www.agilewebdevelopment.com/](http://www.agilewebdevelopment.com/) – Resources for the Rails community

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 28, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276/5 "2006-07-28T16:25:46Z")

</div>

We had a similar problem. So we put all our core business logic into a  
set  
of shared models, bindled them as a plugin, and distribute them that way  
between the applications that share them. This seems to be a little  
cleaner  
then svn:externals when you want to share some models but not all of  
them.

There’s a discussion of this in Rails Recipes, if you can get a copy.  
But  
making a basic plugin is dead simple:

./script/generate plugin my\_plugin\_name

will build the plugin structure in vendor/plugins. Then just dump all  
your  
models into the lib directory. Everything there will be included  
automagically by your app.

HTH,  
Jeff

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 29, 2006, 4:00am UTC](https://www.ruby-forum.com/t/best-practice-for-sharing-code-between-rails-apps/100276/6 "2006-07-29T04:00:59Z")

</div>

> will build the plugin structure in vendor/plugins. Then just dump all your  
> models into the lib directory. Everything there will be included  
> automagically by your app.

Thank you for the tip. I was under the impression that there would be  
some performance problems with plugins but I will try it out and see  
for myself.

I thought of one other way which is to use REST or Web services. Run  
one rails app that contains the shared functionality and interact with  
it via web services from your other rails apps. That might be slower  
still but it would certainly work and it would keep things pretty well  
separated.

Is there a message que or some sort of a corba like interop service  
for ruby? Hopefully one that’s much simpler.
