Created
June 15, 2012 07:35
-
-
Save kechol/2935221 to your computer and use it in GitHub Desktop.
insert rails seeds from google spreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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