Seeding Ruby on Rails App with MYSQL dump in CSV format
bin/rails generate model Damageinspections ticketid:integer date:datetime address:string photo:string
bin/rails db:migrate
rails db:seed
bin/rails console
Create /lib/seeds/ directory and add the csv file.
require 'csv'
csv_text = File.read(Rails.root.join('lib', 'seeds', 'damage_data.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
csv.each do |row|
t = Damageinspection.new
t.ticketid = row['id']
t.date = row['date']
t.address = row['address']
t.photo = row['photo']
t.save
puts "#{t.ticketid} saved"
end
New Comment