However you cut it, sometimes there are differences between the development and production environments.

For example, if you are integrating your website with PayPal, you probably want your local tests in the development environment to hit https://developer.paypal.com/, and not the main site at https://www.paypal.com/.

In my applications, I always store the URL to the PayPal service in a Setting or Configuration object. This allows me the flexibility of changing the value whenever I need to.

Of course, I use Migrations to generate my tables, or pre-populate them with data. Here is a typical migration:

db/migrate/023_setup_paypal_config.rb
 1 class SetupPaypalConfig < ActiveRecord::Migration
 2   def self.up
 3     config = Configuration.find_or_create_by_name('paypal.url')
 4     config.value = 'https://www.paypal.com/'
 5     config.save!
 6   end
 7 
 8   def self.down
 9     Configuration.find_or_create_by_name('paypal.url').destroy
10   end
11 
12   class Configuration < ActiveRecord::Base; end
13 end

Nothing prevents me from keying off RAILS_ENV like this:

1 config.value = case RAILS_ENV
2                when 'development'
3                  'https://developer.paypal.com/'
4                else
5                  'https://www.paypal.com/'
6                end
7 config.save!

An alternative would be to store the PayPal URL in a constant, and initialize the constant in config/environments/*.rb. This means much less code. But in my case, I usually have a need for a configuration-like object where admins of the system can change some values.

Search

Your Host

A picture of me

I am François Beausoleil, a Ruby on Rails and Scala developer. During the day, I work on Seevibes, a platform to measure social interactions related to TV shows. At night, I am interested many things. Read my biography.

Top Tags

Books I read and recommend

Links

Projects I work on

Projects I worked on