# What includes do I need in a Ruby script to have access to Rails methods?

**URL:** <https://www.ruby-forum.com/t/what-includes-do-i-need-in-a-ruby-script-to-have-access-to-rails-methods/174985>\
**Category:** Rails\
**Created:** [September 5, 2009, 7:46am UTC](https://www.ruby-forum.com/t/what-includes-do-i-need-in-a-ruby-script-to-have-access-to-rails-methods/174985 "2009-09-05T07:46:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 7:46am UTC](https://www.ruby-forum.com/t/what-includes-do-i-need-in-a-ruby-script-to-have-access-to-rails-methods/174985/1 "2009-09-05T07:46:30Z")

</div>

Hi,

BACKGROUND: I have a Ruby file I use via cron that makes use of my  
Ruby on Rails application code (e.g. it uses a model file to create  
records in the database). When I run this via cron I use  
“./script/runner filename.rb” and this works fine.

QUESTION: I want to be able to run this file by itself (without  
./script/runner) so I can debug it in Netbeans (e.g. right-click on  
the file and go DEBUG). What #includes’s do I need to ensure the file  
inherits what is required?

Note: I did try including the first two lines in the ./script/runner  
script in my, however this doesn’t seem to work. It just gives a “Run  
‘/Users/greg/source/bankalerts/lib/alerts/alert.rb -h’ for help.”

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 7:57am UTC](https://www.ruby-forum.com/t/what-includes-do-i-need-in-a-ruby-script-to-have-access-to-rails-methods/174985/2 "2009-09-05T07:57:37Z")

</div>

On Sep 5, 1:44 am, Greg H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> inherits what is required?
> 
> Note: I did try including the first two lines in the ./script/runner  
> script in my, however this doesn’t seem to work. It just gives a “Run  
> ‘/Users/greg/source/bankalerts/lib/alerts/alert.rb -h’ for help.”
> 
> –  
> Greghttp://blog.gregnet.org/

To load the whole Rails environment,

require File.join(File.dirname( **FILE** ), “…”, “config”, “boot”)  
#adjust the path accordingly for the location of your script  
require File.join(Rails.root, “config”, “environment”)

If you just need to work with your ActiveRecord models, just require  
‘active\_record’, whatever models you need to work with, and load your  
database.yml.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 5, 2009, 8:23am UTC](https://www.ruby-forum.com/t/what-includes-do-i-need-in-a-ruby-script-to-have-access-to-rails-methods/174985/3 "2009-09-05T08:23:57Z")

</div>

2009/9/5 pharrington [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > QUESTION: I want to be able to run this file by itself (without
> 
> database.yml.
> 
> >

excellent, thanks pharrington, worked like a charm

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 7, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/what-includes-do-i-need-in-a-ruby-script-to-have-access-to-rails-methods/174985/4 "2009-09-07T12:45:39Z")

</div>

You don’t need to include (require) boot.rb as it’s already included in  
environment.rb.

Thanks,  
Abhinav

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)
