Howto: Setup The Rails Environment In A .rb Script
Join the DZone community and get the full member experience.Join For Free
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..
Opinions expressed by DZone contributors are their own.