##csv
user_data.csv
id,name,email,created_at,updated_at
1,user1,example.com,2018-02-17 00:00:00,2018-02-17 00:00:00
2,user2,example.com,2018-02-17 00:00:00,2018-02-17 00:00:00
3,user3,example.com,2018-02-17 00:00:00,2018-02-17 00:00:00
4,user4,example.com,2018-02-17 00:00:00,2018-02-17 00:00:00
##ruby
import.rb
require 'csv'
CSV.read("user_data.csv", headers: true).each do |row|
user = User.create!(
id: row['id'],
name: row['name'],
email: row['email'],
created_at: row['created_at'],
updated_at: row['updated_at']
)
end
###実行!
$ rails runner import.rb
## 問題
N+1になってしまうので数が多い場合は、sequel
、activerecord-import
などのgemを使ってbulkinsertする