# Using Rails App (Models) in my Ruby script

**URL:** <https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047>\
**Category:** Ruby\
**Created:** [September 19, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047 "2008-09-19T20:10:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/1 "2008-09-19T20:10:47Z")

</div>

Hi, All.

I’m writing a script that’s going to run on schedule to pull some info  
from a db and dump it into a csv to be ftped

And a similar script to process a csv/excel file we get from a vendor to  
put in our db.

I can use like Ruby DBI or something.  
But I’d much rather use ActiveRecord. Here is the question.

How do I let my ruby script know to include all of my rails goodies.  
Like the Model’s I already created?

I tried to write a sample where I connect to a db using activerecord and  
it works, but it looks like I will have to recreate all of my models in  
my Ruby script? Which is not a biggie - jsut copy and paste, but I’d  
hate to maintain 2 versions of models.

Also will I run into any problems with my restfull\_authentication plugin  
doing that if it is possible to do at all (this might be a question for  
a Rails list thou)

Thank you

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/2 "2008-09-19T21:57:31Z")

</div>

Nick da G wrote:

> Hi, All.
> 
> I’m writing a script that’s going to run on schedule to pull some info  
> from a db and dump it into a csv to be ftped
> 
> And a similar script to process a csv/excel file we get from a vendor to  
> put in our db.
> 
> I can use like Ruby DBI or something.  
> But I’d much rather use ActiveRecord. Here is the question.
> 
> How do I let my ruby script know to include all of my rails goodies.  
> Like the Model’s I already created?

script/runner

(also script/console if you want to have an IRb shell with all the rails  
goodies loaded)

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/3 "2008-09-19T22:05:59Z")

</div>

On Fri, Sep 19, 2008 at 1:03 PM, Nick da G [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> a Rails list thou)  
> You might be able to simple load them (load ‘/dir\_path/my\_model.rb’).  
> You can simply get a list of the Model file names, and iterate through  
> them, loading each one. I’ve tested Models in irb like that. YMMV.

Todd

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/4 "2008-09-19T22:08:32Z")

</div>

Brian C. wrote:

> Nick da G wrote:
> 
> > Hi, All.
> > 
> > I’m writing a script that’s going to run on schedule to pull some info  
> > from a db and dump it into a csv to be ftped
> > 
> > And a similar script to process a csv/excel file we get from a vendor to  
> > put in our db.
> > 
> > I can use like Ruby DBI or something.  
> > But I’d much rather use ActiveRecord. Here is the question.
> > 
> > How do I let my ruby script know to include all of my rails goodies.  
> > Like the Model’s I already created?
> 
> script/runner
> 
> (also script/console if you want to have an IRb shell with all the rails  
> goodies loaded)

Hi Brian, thanks for the reply but I guess I didn’t make myself clear.

script/console would load an interactive console - that’s waiting for  
my input. Not sure how to I would interact with that from my ruby progam  
and how to load it from my ruby script.

I looked into script/runner - but as I understand it is for running  
Background processing from your rails app - like when you want to  
process an uploaded video. In anyways I was not able to find a primer  
that would show how to connect to it from my ruby script - the one that  
is produced from script/runner -h - doesn’t work for me on Mac OS and I  
can’t find any sensible tutorials or examples.

What I want is to be able to do is : just load some models from my rails  
app. Like Product model. so that in my Ruby script I can just do p =  
Product.find(2), instead of running DBI code.

Hope that’s clearer.

P.S.:  
Brian do you know of any links or books that would talk more about  
scrip/runner - I obviously already looked at api and rails wiki.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/5 "2008-09-19T23:20:26Z")

</div>

Nick da G wrote:

> Hi, All.
> 
> I’m writing a script that’s going to run on schedule to pull some info  
> from a db and dump it into a csv to be ftped
> 
> And a similar script to process a csv/excel file we get from a vendor to  
> put in our db.

just require boot.rb and environment.rb (don’t forget to set rails\_env  
to  
production/development first) from your scrips, and all your models will  
be  
available in your ruby script.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/6 "2008-09-19T23:50:42Z")

</div>

Ok I just did a search on Ruby Forums and I think I found what I needed.  
Thank Brian for sending me in a right direction.

It also looks like my error was related to Mac OS . I tried same code at  
work on my XP box and it works now.  
Damn I thought I was going crazy.

Thanks for the help thou.

for anyone else looking to setup scheduling on a Windows box for a ruby  
script.

Just make your task call  
(pass it environment if you want)(and then pass it your great ruby  
script - make sure you use aboslute paths )

ruby c:\path to your great rails app\script\runner c:\my great ruby  
scripts folder\cron.rb  
(or whatever you want to call it. I would suggect to use no spaces in  
the folder names - other wise you might need to do fiddle with the extra  
quotes and it’s annoying)

Alternatively you can setup a .bat file ( if you want to do some extra  
stuff in windows batch mode - like driving yourself crazy or something)

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/7 "2008-09-19T23:57:19Z")

</div>

On Sep 19, 2008, at 4:12 PM, matu wrote:

> > put in our db.
> 
> just require boot.rb and environment.rb

environment.rb required boot.rb for you, so it’s all you need.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 19, 2008, 11:53pm UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/8 "2008-09-19T23:53:52Z")

</div>

Also this method works cool for scheduled PDF printing - just printed my  
first automated invoice, generated from rails to PDF:Writer to color  
printer) - work like a charm. I might need to switch it to Linux later,  
if we do a lot of heavy printing.

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 20, 2008, 12:06am UTC](https://www.ruby-forum.com/t/using-rails-app-models-in-my-ruby-script/149047/9 "2008-09-20T00:06:54Z")

</div>

James & matu

thanks for pointers
