Skip to content

Instantly share code, notes, and snippets.

@gustavoguichard
Created December 3, 2013 23:58
Show Gist options
  • Save gustavoguichard/7779947 to your computer and use it in GitHub Desktop.
Save gustavoguichard/7779947 to your computer and use it in GitHub Desktop.
Seed example
# utf-8
# Migrate data from old database
old_database = File.read(Rails.root.join('db/old_database.sql'))
ActiveRecord::Base.connection.execute old_database
convert_old_data = File.read(Rails.root.join('db/convert_old_data.sql'))
ActiveRecord::Base.connection.execute convert_old_data
# Create first access tokens
User.all.each do |u|
u.first_access_token = u.id.to_s + Devise.friendly_token
u.save!
end
# Create continents
[
{ name: 'Africa' },
{ name: 'South America' },
{ name: 'Central America' },
{ name: 'North America' },
{ name: 'Europe' },
{ name: 'Asia' },
{ name: 'Oceania' }
].each do |c|
continent = Continent.find_or_initialize_by_name c[:name]
continent.update_attributes({
name: c[:name]
})
end
# Assign continent to each country in the database
%w(Thailand Philipinnes India China Indonesia).each {|name| country = Country.find_by_name(name); country.continent = Continent.find_by_name("Asia"); country.save!}
%w(Portugal Spain Italy France Deutschland England Turkey Netherlands Hungary Poland).each {|name| country = Country.find_by_name(name); country.continent = Continent.find_by_name("Europe"); country.save!}
%w(USA Canada Mexico).each {|name| country = Country.find_by_name(name); country.continent = Continent.find_by_name("North America"); country.save!}
%w(Brazil Argentina Uruguay Colombia).each {|name| country = Country.find_by_name(name); country.continent = Continent.find_by_name("South America"); country.save!}
%w(Panama).each {|name| country = Country.find_by_name(name); country.continent = Continent.find_by_name("Central America"); country.save!}
Friend.where("video_url IS NOT NULL").each {|f| f.video_url = "http://www.youtube.com/watch?v=#{f.video_url}"; f.save!}
Friend.where(video_url: "http://www.youtube.com/watch?v=").each {|f| f.video_url = nil; f.save!}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment