davidb
davidb

Reputation: 8954

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?

Upvotes: 1

Views: 174

Answers (3)

tbuehlmann
tbuehlmann

Reputation: 9110

I think you want either Rails.env = 'production' or ENV['RAILS_ENV'] = 'production' in your script.

Upvotes: 1

Ben Walding
Ben Walding

Reputation: 4054

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.

Upvotes: 2

Pritesh Jain
Pritesh Jain

Reputation: 9146

@davidb, I am not sure what u want also not as good on rails yet but may be you can use the seed.rb (i.e seed functionality) for achieving what you want if this script is run only once or sometimes as we can specify the environment while running seed

rake db:seed RAILS_ENV=production

Upvotes: 0

Related Questions