# Sharing classes in rails and with other ruby scripts

**URL:** <https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071>\
**Category:** Ruby\
**Created:** [August 17, 2008, 7:30am UTC](https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071 "2008-08-17T07:30:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [August 17, 2008, 7:30am UTC](https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071/1 "2008-08-17T07:30:42Z")

</div>

Hey there all,

I have several Ruby classes that I need to share between several  
applications.  
Our company collects information from industrial machines, processes it,  
and  
alerts our customers of system changes. This is either by text message,  
website, etc…  
So i have a ruby on rails website built that allows them to see whats  
up.  
Then, we have other companies we build info sites on, kinda based on the  
same code, but private labled, and modified in other ways too.

Here is what I would like, we have several websites that all have a  
EquipmentSensor model, a ruby class of type ActiveRecord::Base, we also  
have  
a folder of models that we ‘require’ to use in our data processing  
software,  
and in our cron jobs, etc…

Is there a way i can share this class ( model ) with several different  
websites running rails and also use it in my cron jobs, etc…

thanks for any tips.

sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [August 19, 2008, 3:17am UTC](https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071/2 "2008-08-19T03:17:50Z")

</div>

Shawn B. wrote:

> Is there a way i can share this class ( model ) with several different  
> websites running rails and also use it in my cron jobs, etc…

Well, ActiveRecord is available as it’s own gem, and a class which  
inherits from ActiveRecord does not need the full Rails framework to be  
useful (a bit of Googling should give you examples of ActiveRecord used  
outside of Rails…or you could just look at Camping:  
[http://code.whytheluckystiff.net/camping](http://code.whytheluckystiff.net/camping)). If you wanted, you could keep  
your model collection in one place (say, /usr/lib/mycompanysstuff), and  
then you would just need to modify your environment to require the model  
classes from there…

…however! If you are constantly accessing the models from Rails apps,  
it would probably be worth your while to look into setting up a  
dedicated Rails app to serve up the model data and then access the data  
from your other apps using ActiveResource. That solution has the  
advantage of being scalable as well (…and ActiveResource is just dang  
cool!).

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [August 19, 2008, 5:54am UTC](https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071/3 "2008-08-19T05:54:00Z")

</div>

Thanks for this, I am using these models in apps that are not rails apps  
andy yes, It is pretty easy to do. I had some advice in the rails irc  
where  
someone recommended the ActiveResource way to go. However, after having  
read  
some on it, i don’t think i can employ it here because it is pretty new  
and  
my rails version is 1.2.4 If i upgrade to rails 2, i run the risk of  
breaking my site (which is pretty large now) .

Thanks for your attention on this Joshua, and if you know of a good  
trick  
that might work for me I would welcome any suggestions. Actually, it is  
only about 4 of the models that i want to share, but if i do 4, i may as  
well do them all.

thanks again  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [August 19, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071/4 "2008-08-19T15:42:15Z")

</div>

cool, thanks for this.  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [August 19, 2008, 7:34am UTC](https://www.ruby-forum.com/t/sharing-classes-in-rails-and-with-other-ruby-scripts/146071/5 "2008-08-19T07:34:29Z")

</div>

Shawn B. wrote:

> Thanks for your attention on this Joshua, and if you know of a good  
> trick that might work for me I would welcome any suggestions.

Well, I think it’s important to remember that Rails is still Ruby, so  
all the good ol’ Ruby tricks still work. In other words, you can always  
pull out common code into a lib and ‘require’ it. This would even work  
for ActiveRecord subclasses.
