Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Feb 14

Howto: setup the rails environment in a .rb script

Posted by Sandro Paganotti in Ruby on Rails - comments are closed digg this add to delicious

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.

Comments

  • fallenrogue

    Posted on February 15

    why not just use rake? Create a rake task (or series of) for your processing then if you need to repeat it, create a small script that fires it when you need it.
  • edbond

    Posted on February 15

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

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory