How do I run a Ruby file in a Rails environment?

I want to run a Ruby file in the context of a Rails environment. rails runner almost does what I want to do, but I'd like to just give it the file name and arguments. I'm pretty sure this is possible since I've done it before. Can someone remind me how to do this?


The simplest way is with rails runner because you don't need to modify your script.

http://guides.rubyonrails.org/command_line.html#rails-runner

Just say rails runner script.rb


Simply require environment.rb in your script. If your script is located in the script directory of your Rails app do

require File.expand_path('../../config/environment', __FILE__)

You can control the environment used (development/test/production) by setting the RAILS_ENV environment variable when running the script.

RAILS_ENV=production ruby script/test.rb