Different data per environment in migrations
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class SetupPaypalConfig < ActiveRecord::Migration def self.up config = Configuration.find_or_create_by_name('paypal.url') config.value = 'https://www.paypal.com/' config.save! end def self.down Configuration.find_or_create_by_name('paypal.url').destroy end class Configuration < ActiveRecord::Base; end end |
Nothing prevents me from keying off RAILS_ENV like this:
1 2 3 4 5 6 7 |
config.value = case RAILS_ENV when 'development' 'https://developer.paypal.com/' else 'https://www.paypal.com/' end 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.