rails runner feature of the Ruby on Rails command line interface is pretty awesome.
The documentation states:
runnerruns Ruby code in the context of Rails non-interactively.
Use it like the
ruby command to execute Ruby code in the context of your Rails environment. Take this file:
# rails_runner_in_action.rb puts Developer.count # 5 puts Post.count # 40 puts Channel.count # 17
And run it with:
$ rails runner rails_runner_in_action.rb 5 40 17
It also runs Ruby code right in the terminal, so this works (
rails r is an alias):
$ rails r "puts Developer.count" 5