Skip to content

Instantly share code, notes, and snippets.

@kechol
Created June 15, 2012 07:35
Show Gist options
  • Save kechol/2935221 to your computer and use it in GitHub Desktop.
Save kechol/2935221 to your computer and use it in GitHub Desktop.
insert rails seeds from google spreadsheet
require 'rubygems'
require 'google_drive'
# set your username and password
login = GoogleDrive.login(GoogleDrive::USERNAME, GoogleDrive::PASSWORD)
# set your spreadsheet key
sheet = login.spreadsheet_by_key(GoogleDrive::SPREADSHEET)
sheet.worksheets.each do |ws|
p ws.title
model = eval(ws.title)
columns = model.column_names
rows = ws.rows.dup
fields = rows.shift.map{ |k| k[/\w+/] }
data = []
rows.each do |val|
ary = [fields.dup, val.dup].transpose
ary.select!{ |a| columns.include?(a[0]) }
data.push(Hash[*ary.flatten]) if ary
end
model.create!(data, { without_protection: true }) unless data.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment