120

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?

iltempo
  • 15,718
  • 8
  • 61
  • 72
weicool
  • 2,613
  • 5
  • 22
  • 19

5 Answers5

158

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

runner runs Ruby code in the context of Rails non-interactively.

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

Just say rails runner script.rb

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Ryan Porter
  • 1,982
  • 1
  • 11
  • 17
  • 2
    If you make the script executable by running `chmod +x script.rb`, you can add `#!/usr/bin/env rails runner` to the top of the script, and then simply run it with `./script.rb`. – stwr667 Sep 16 '20 at 06:30
39

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
iltempo
  • 15,718
  • 8
  • 61
  • 72
  • If i run above code it will give "home/apps/config/environment". What if i have 2 rails app running on the same directory. Which environment will it take? i am facing this problem in my local box. kindly suggest – Vijay Sali May 05 '14 at 07:26
  • @VijaySali I assume that you are running your script from the `app/script` directory. The environment file will be taken from the individual app. – iltempo May 05 '14 at 13:11
  • yup thanks it worked for me , I added ENV['RAILS_ENV'] as well – Vijay Sali May 06 '14 at 11:46
30

Runner runs Ruby code in the context of Rails non-interactively.

From rails runner command:

Usage: runner [options] ('Some.ruby(code)' or a filename)

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

    -h, --help                       Show this help message.

You can also use runner as a shebang line for your scripts like this:

-------------------------------------------------------------
#!/usr/bin/env /Users/me/rails_project/script/rails runner

Product.all.each { |p| p.price *= 2 ; p.save! }
-------------------------------------------------------------
JZ.
  • 21,147
  • 32
  • 115
  • 192
colsen
  • 573
  • 5
  • 13
8

This is an old question, but in my opinion I often find it helpful to create a rake task... and it's actually very easy.

In lib/tasks/example.rake:

namespace :example do

desc "Sample description you'd see if you ran: 'rake --tasks' in the terminal"
task create_user: :environment do
  User.create! first_name: "Foo", last_name: "Bar"
end

And then in the terminal run:

rake example:create_user

Locally this will be run in the context of your development database, and if run on Heroku it will be run while connected to your production database. I find this especially useful to assist with migrations, or modified tables.

Matt
  • 5,800
  • 1
  • 44
  • 40
0

You just need:

bundle exec rails r ~/my_script.rb
duhaime
  • 25,611
  • 17
  • 169
  • 224