【Rails】seedsディレクトリ配下のseedファイルを全てrequireしたい
超久しぶりにQiita投稿。
備忘録です。
seedファイルを分割して管理している場合、db/seeds.rb
でいちいちrequireしてあげないと bundle exec rails db:seed
した際に一括でseedを流してくれない。
現状が以下。
seeds.rb
require './db/seeds/user.rb'
require './db/seeds/article.rb'
require './db/seeds/book.rb'
・
・
・
可読性悪いし、requireが続いていくので書くのが毎回面倒。
なので、ワイルドカードを使う感じでうまいことrequireできるようにする。
進化したseeds.rb
が以下。
seeds.rb
Dir[File.expand_path('./db/seeds' << '/*.rb')].each do |file|
require file
end
まあやってることは変わらんですが、これで毎回requireを書かなくてよくなりました。
おしまい。
Author And Source
この問題について(【Rails】seedsディレクトリ配下のseedファイルを全てrequireしたい), 我々は、より多くの情報をここで見つけました https://qiita.com/motty93/items/02a9599369d9e7babd01著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .