Search code examples
ruby-on-railsrubyruby-on-rails-3.1

How to write ruby script that uses the production environment instead of development?


I have writen a ruby script that requires config/environment.rb so I can use all the models of my rails application in the script but the problem is that I want to use the production environment instead of the develoment environment which seems to be the default behavior.

Im using Rails 3.1.1 and Ruby 1.9.2

How can I run the script with the production environment?


Solution

  • Your script will take the environment variable RAILS_ENV as the environment to use.

    I'd be very wary of overriding that in the script as it may cause much confusion if you try and run your script in another environment - e.g. staging - and it starts trying to access production databases etc.

    So do either:

    RAILS_ENV=production ./script/my-awesome-script
    

    or

    export RAILS_ENV=production
    ./script/my-awesome-script
    

    Generally speaking; when I log into a production Rails environment I'd be changing the environment straight away if I haven't configured it to be "production" by default.