Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

コピペでCSVファイルをデータベースに入力する(Ruby, Ruby on Rails)

Last updated at Posted at 2021-07-23

1.railsのdbフォルダーののなかにCSVファイルを入れる!

2.db/seedsrbに書き込み!

db/seeds.rb
require "csv"

#「header: true」とするとCSVの1行目の行情報でハッシュのように扱える
CSV.foreach('CSVファイルのパス',headers: true) do |row|
  モデル名.create(
    カラム名:     row['csvファイルの列'],#row[0]のように列指定もできる
    カラム名:     row['csvファイルの列']
  )
end

3.ターミナルで「rake db:seed」と打ち込む!

ターミナル
% rake db:seed

これで完成!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Comments

No comments

Let's comment your feelings that are more than good

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?