The RunnerScript is used to run ruby within your application’s environment.
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.
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 _
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
...
script/runner "Agent.build(\"$REPOS\", $REV)"
mailman: "|/path/to/app/script/runner 'Mailman.receive(STDIN.read)'"
script/runner 'load "db/fill_user_table.rb"'
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 }"'