Ruby on Rails
RunnerScript

The RunnerScript is used to run ruby within your application’s environment.

Usage


Usage: runner 'puts Person.find(1).name' [options]

    -e, --environment=name           Specifies the environment for the runner 
                                     to operate under (test/development/production).
                                     Default: development

    -h, --help                       Show this help message.      

Alternative Usage

Note that some users have experienced errors attempting to set the rails environment in the manner shown above. The environment can also be set as follows:


RAILS_ENV=production ./script/runner System.somemethod

http://www.theblatherskite.com/articles/2007/04/03/setting-environment-for-script-runner

Examples


script/runner 'CurrencyService.update_records' -e development

Note: These methods must be created within models. The runner script does not load any controllers. Controllers are associated with an HTTP request. An HTTP request does not exist in the command-line environment.

A method should be named in the following format: Class_name.method_name. You must also use the keyword self in the method definition, thus making it a class method. This is because, when you call the method, an instance of the object does not yet exist, so we can only use class methods but not instance methods. For example, a method located in a model Test should be listed as follows:


class Test 
  def self.new_method
    # Do interesting things
  end
end

This can be called from the command line as:


script/runner 'Test.new_method' -e development

From How To Use CIA For Continuous Integration:


...
script/runner "Agent.build(\"$REPOS\", $REV)" 

From HowToReceiveEmailsWithActionMailer:


mailman: "|/path/to/app/script/runner 'Mailman.receive(STDIN.read)'" 

Run a file:


script/runner 'load "db/fill_user_table.rb"'

Delete old sessions (1 day):

You can specify the sessions db with a special environment. Run as cronjob.


script/runner 'class Session < ActiveRecord::Base; end; Session.destroy_all "updated_at <= #{ Time.now.to_i - 1.day }"'

See also