Martyn
Martyn

Reputation: 6403

migrate data in rails when deploying

I have two tables I want to populate with data - countries, and cities. The user doesn't add/update these tables. I want them to be part of a migration so when I deploy to Heroku the data is transferred too. Up until now, I've only been migrating structure (not data). Is this possible?

Upvotes: 1

Views: 41

Answers (2)

Alessandro De Simone
Alessandro De Simone

Reputation: 4215

As emaillenin said those data are seeds, anyway you can use a migration if you want, nothing difficult:

class ImportCountriesAndCities < ActiveRecord::Migration
  def self.up
    import_countries_and_cities
  end

  def self.down
    remove_countries_and_cities
  end

private
  def self.import_countries_and_cities
    ..
  end

  def self.remove_countries_and_cities
    ...
  end

end

Upvotes: 0

Raj
Raj

Reputation: 22956

Yes it is possible. Populate your initial data in db/seeds.rb like this:

Country.create(name: 'Germany', population: 81831000)
Country.create(name: 'France', population: 65447374)
Country.create(name: 'Belgium', population: 10839905)
Country.create(name: 'Netherlands', population: 16680000)

and do rake db:seed in production to load data from your seeds.

Tutorial.

Upvotes: 1

Related Questions