How to Set the Rails Environment for My Somewhat Stand Alone Ruby Script

How can I set the Rails environment for my somewhat stand alone Ruby script?

If you're going to be using the rails environment, your best bet would be to make this a rake script. To do this, put a twitter.rake file into lib/tasks and begin and end it like this:

task(:twitter_load => :environment) do
# your code goes here
end

That way, you're doing it by "following conventions" and it doesn't have that 'orrible smell associated with it.

How to set rails_env for a script or batch file

To initialise the Rails environment, instead of putting

require "#{File.dirname(__FILE__)}/../config/environment.rb"

launch your batch file using script/runner and specify the environment with the -e option

e.g.

script/runner -e production lib/batch.rb

I think the above is The Rails Way of writing and executing a script that needs the Rails framework initialised in order to work. The alternative, as neutrino says, is to prefix the command with RAILS_ENV=value e.g.

$ RAILS_ENV=production lib/batch.rb

This is a standard shell feature to set an environment variable prior to executing a command.

Set environment to development in ruby on rails 4

This is normal. Bundler is a general-purpose dependency manager for Ruby. It has no idea that Rails exists. The group directives are exposing Bundler's groups feature, not a function of Rails.

If you don't instruct Bundler otherwise, it will install every gem from every group. It doesn't know what groups you do and don't want installed; it just knows that you defined some groups.

If you don't want to install all of your gems (or can't install all of your gems), you can skip production:

bundle install --without production

Similarly, you can skip development and testing gems when you deploy:

bundle install --without development test

(This is how, for example, Heroku and Cloud66 install only the gems you need for production.)

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

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.

What's the standard way of specifying the environment for a Ruby app without using RAILS_ENV?

You are free to invent your own semantics, and your own ways of determining which configuration is in use. The environment names of test, development, production have become well-known standards, sometimes with the addition of release-management steps such as smoke, uat, staging etc. However, there is no requirement to use environments as a concept in the first place, nor is there a generic approach that could be applied across all Ruby projects - the set of possible applications is too broad.

If you are creating a web application that conforms to the rack API (for hosting in Apache/Passenger or Thin or other server that supports Rack), it is common to use RACK_ENV environment variable to control the choice of named environment (and which part of config to then use) - Sinatra config will use this for example, and Rails will fall back to it.

How do I force RAILS_ENV=development from within environment.rb or initializers?

Why don't you change your production.rb config to match the settings found in development.rb?

run a script in production

RAILS_ENV=production rails r script/foo.rb

How do I run a ruby script, that I put in my /lib/tasks/ directory in my Rails app, once?

The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).

Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.

So, create a rake task: whatever_name.rake

Example:

desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end

def awesome_method
#put your ruby code here
end

Now from your command prompt, type rake do_awesome_stuff to execute this rake task.

To make it boot Rails, change task definition to this:

task :do_awesome_stuff => :environment do

Settings returns nil for all configurations which reads from environment variables in devise.rb

Created a yml file with key as environment variable name. Then added environment variables before module declaration in application.rb

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

if Rails.env.production?
config_file = '/srv/shared_filesystem/pdf-config.yml'
if File.exists?(config_file)
config = YAML.load(File.read(config_file))
config.each do |key, value|
ENV[key] ||= value.to_s unless value.kind_of? Hash
end
else
raise 'Missing required configuration file /srv/shared_filesystem_ro/pdf-config.yml'
end
end

module MyApp
class Application < Rails::Application
.. ..
.. ..
end
end


Related Topics



Leave a reply



Submit