# External Model Definition Using ActiveRecord

**URL:** <https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911>\
**Category:** Ruby\
**Created:** [August 18, 2009, 6:25am UTC](https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911 "2009-08-18T06:25:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elizabeth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Elizabeth](https://www.ruby-forum.com/u/Elizabeth)\
**Post date:** [August 18, 2009, 6:25am UTC](https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911/1 "2009-08-18T06:25:44Z")

</div>

I’ve been Googling this topic, and I haven’t found the answer.

In Rails, the Models are defined in external files. How do you use  
externally defined models in Ruby using ActiveRecord. I have a rails  
app and a ruby app which will share the model definitions. I want to  
use the DRY technique, so I don’t want to define them both in the  
models directory and inside the Ruby application.

Thanks in advance,  
Elizabeth

---

<div class="post-metadata">

**Author:** ![Elizabeth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Elizabeth](https://www.ruby-forum.com/u/Elizabeth)\
**Post date:** [August 18, 2009, 12:32pm UTC](https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911/2 "2009-08-18T12:32:49Z")

</div>

Elizabeth wrote:

> I’ve been Googling this topic, and I haven’t found the answer.
> 
> In Rails, the Models are defined in external files. How do you use  
> externally defined models in Ruby using ActiveRecord. I have a rails  
> app and a ruby app which will share the model definitions. I want to  
> use the DRY technique, so I don’t want to define them both in the  
> models directory and inside the Ruby application.

At simplest, just load them:

require ‘rubygems’  
require ‘active\_record’  
require ‘app/models/foo’  
require ‘app/models/bar’

However you still need to establish\_connection to the database in your  
app.

Alternatively you may decide to bootstrap the entire Rails environment,  
which you can do most easily by running your script like so:

script/runner myscript.rb

Then it will use the config/database.yml entries to connect to the  
database as well.

Also, you can use script/console to get an irb session with the models  
already wired up.

---

<div class="post-metadata">

**Author:** ![Elizabeth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Elizabeth](https://www.ruby-forum.com/u/Elizabeth)\
**Post date:** [August 18, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911/3 "2009-08-18T19:06:14Z")

</div>

On Aug 18, 3:32 am, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘rubygems’  
> require ‘active\_record’  
> require ‘app/models/foo’  
> require ‘app/models/bar’

Thank you! I come from Perl, so I kept thinking use lib. I can  
probably look it up, but since you’re here, is require ‘app/models/\*’  
possible?

> However you still need to establish\_connection to the database in your  
> app.
> 
> Alternatively you may decide to bootstrap the entire Rails environment,  
> which you can do most easily by running your script like so:
> 
> script/runner myscript.rb
> 
> Then it will use the config/database.yml entries to connect to the  
> database as well.

script/runner sounds like it will load a Ruby script using the rails  
environment variables? I will look this up! This sounds even more  
like what I need!

> Also, you can use script/console to get an irb session with the models  
> already wired up.

🙂 Too simple, but irb is definitely an under-utilized tool.

> –  
> Posted viahttp://www.ruby-forum.com/.

Thank you very much! The simple answers are the best, and I very much  
think that these will do it.

Elizabeth

---

<div class="post-metadata">

**Author:** ![Elizabeth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Elizabeth](https://www.ruby-forum.com/u/Elizabeth)\
**Post date:** [August 18, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911/4 "2009-08-18T21:01:53Z")

</div>

Elizabeth wrote:

> Thank you! I come from Perl, so I kept thinking use lib. I can  
> probably look it up, but since you’re here, is require ‘app/models/\*’  
> possible?

No, you would have to do it yourself, e.g.

Dir[“app/models/\*.rb”].each do |f|  
require File.basename(f, “.rb”)  
end

> script/runner sounds like it will load a Ruby script using the rails  
> environment variables?

Yes. If you have more detailed questions specifically about Rails  
features rather than about the Ruby programming language, they would be  
best asked on a Rails mailing list.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![Elizabeth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/34f0e0/32.png) [@Elizabeth](https://www.ruby-forum.com/u/Elizabeth)\
**Post date:** [August 19, 2009, 7:12am UTC](https://www.ruby-forum.com/t/external-model-definition-using-activerecord/173911/5 "2009-08-19T07:12:39Z")

</div>

On Tue, 2009-08-18 at 13:25 +0900, Elizabeth wrote:

> I’ve been Googling this topic, and I haven’t found the answer.
> 
> In Rails, the Models are defined in external files. How do you use  
> externally defined models in Ruby using ActiveRecord. I have a rails  
> app and a ruby app which will share the model definitions. I want to  
> use the DRY technique, so I don’t want to define them both in the  
> models directory and inside the Ruby application.

I did that by writing a small script that simply copies all models from  
rails, puts them into a namespace and generates an initial ‘require  
file’ to autoload them in a rubygems environment.  
On the target system then I just install the gem and can reuse all  
models in any ruby script.  
If models are changed I run that script again and get a new version of  
the gem.

Martin
