For ad hoc Rails tasks we have a few implementation alternatives, chief among which would seem to be:
script/runner some_useful_thing
and:
rake some:other_useful_thing
Which option should I prefer? If there's a clear favourite then when, if ever, should I consider using the other? If never, then why would you suppose it's still present in the framework without deprecation warnings?
One thing I've done is just write normal ruby scripts and put them in the
script/maintenance
directory.All you need to do to load rails and get access to all your models, etc, is put
require '../../config/environment.rb'
at the top of your file, then you're away.For one off commands script/runner can be fine. For anything repeated, a rake task is easier in the long-run, and has a summary if you forget what it does.
Corrected based on comment 2 down. Give them the karma!
FWIW - Rails 3.0+ changes how you initialize the Rails system in a standalone script.
As mentioned above you can also do:
Or put all the code in a Rake task, but I have a lot of legacy code from Rails 2; so I didn't want to go down that path immediately.
Each has its advantages and disadvantages.
In Rails 3.0+, the
config/environment.rb
requires theconfig/application.rb
, that requires theconfig/boot.rb
.So, to load an app in Rails 3, you still only have to require the
environment.rb
I got the impression script/runner was primarily for periodic tasks. E.g., a cron job that runs:
FWIW there seems to be some movement away from using script runner in favor of rake:
Also, as per this post you can use rake for recurring tasks just fine: