Skip to content

Instantly share code, notes, and snippets.

@ysinc88
Created September 16, 2015 10:21
Show Gist options
  • Save ysinc88/eb969685510716e40b17 to your computer and use it in GitHub Desktop.
Save ysinc88/eb969685510716e40b17 to your computer and use it in GitHub Desktop.
seeding from excel
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
require 'roo'
def fetch_excel_data
ex = Roo::Excel.new("public/rsvp.xls")
ex.default_sheet = ex.sheets[0] #Mention the sheet number (0 is the first sheet, 1 is second sheet, etc.)
2.upto(132) do | line | #Start and end of rows you want to include
first_name = ex.cell(line,'A') #Column A in spreadsheet
last_name = ex.cell(line,'B')
email = ex.cell(line,'C')
phone = ex.cell(line,'D')
rsvp = ex.cell(line,'E')
x = Guest.create(:first_name => first_name, :last_name => last_name, :email => email, :phone => phone, :rsvp => rsvp)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment