LoginSignup
16
21

More than 3 years have passed since last update.

【Rails】seedから画像をdbに保存する方法

Posted at

seedデータからdbに画像を保存するやり方です。
本番環境でもできます。

(db/seeds.rb)

20.times do |n|
 name = Faker::Games::Pokemon.name
 email = Faker::Internet.email
 password = 'password'
 User.create!(
  name: name,
  email: email,
  password: password,
  password_confirmation: password,
  icon: open("./db/fixtures/icon#{n}.jpg")
 )
end

こんな感じで
icon: open("./db/fixtures/icon#{n}.jpg")
で呼び出しています。
自分は(db/fixtures)の中にicon0.jpgicon1.jpgみたいな感じで20個jpgファイルを置きました。
ご参考までに

16
21
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
16
21