[Quicky] Self contained Rails script

[Quicky] Self contained Rails script

To run a script with your Rails application setup use rails runner https://guides.rubyonrails.org/command_line.html#bin-rails-runner

# inline script
./bin/rails runner "puts Rails.env"

# file
./bin/rails runner path/to/file.rb

If you want to have a self container, here's what you need (eg: filename bin/puts_env):

#!/usr/bin/env ruby
APP_PATH = File.expand_path("../../config/application",  __FILE__)
require File.expand_path("../../config/boot",  __FILE__)
require APP_PATH

Rails.application.require_environment!


###### Your script below
puts Rails.env

Don't forget to make the file executable:

chmod +x bin/puts_env

Now you can run the script, no more need to remember how to run it, just do:

./bin/puts_env

Magical!