Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Howto: setup the rails environment in a .rb script (See related posts)

Stolen from: http://www.railsonwave.com/railsonwave/2007/2/14/howto-setup-the-rails-environment-in-a-rb-script
I quote:

It can happen that you need to perform some operations on your rails application from a script, for example you may need to have a cron job that do something on your models. To archieve this goal you need to set up a rails environment in your script; it’s really easy, you’ve just to put these lines on the top of your .rb file.
RAILS_ROOT = RELATIVEPATHTOYOURAPPFOLDER
require RAILS_ROOT + "/config/environment" 
Dependencies.load_file("application.rb")

I really don’t know why but you have to manually load application.rb, if you don’t rails translate the ‘ApplicationController’ constant into ‘application_controller’ and look for a file named ‘application_controller.rb’ that doesn’t exist.

Sandro.

From the comments:
edbond says:
for script in your RAILS_ROOT folder
ENV['RAILS_ENV'] = 'development' require File.expand_path(File.dirname(__FILE__) + "/config/environment")


// insert code here..

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts