user3763682
user3763682

Reputation: 441

Rails Seeding with seed file broken into parts

I am following this example: https://obie.hashnode.dev/rubysinatra-and-organizing-your-seed-files

My directory structure is:

      db < folder >
        seeds < folder >
          01_user_seed.rb
          02_product_seed.rb
          03_images_seed.rb.
          seeds.rb

seeds.rb has the following code (per Obie Munoz's article, slightly modified):

      Dir[Rails.root.join('db', 'seeds', '*.rb')].sort.each do |seed|
        load seed
      end

When I then run rails db:seed, it fails silently. I tried placing raise before and after the line Dir[Rails.root.join('db', 'seeds', '*.rb')].sort.each do |seed|and it doesn't 'raise'.

Do I have the code wrong? Or should the Dir[Rails.root *** be in the config/application.rb file and, if so, what should be in the seeds.rb file.

PS - The 01_user_seed.rb looks like this (straight from Hartl's Ruby on Rails Rails Tutorial):

      p 'Seeding users...'
      1.times do |n|
        name = Faker::Name.name
        email = "ex-#{n+1}@4testing.net"
        password = "pass-#{n+1}"

      User.create!( name: name,
                    email: email,
                    password: password,
                    password_confirmation: password
      )
      p 'Finished seeding users...'
      end

rails db:seed was working with seeds.rb before I reconfigured the code.

Thanks

Upvotes: 1

Views: 55

Answers (1)

smathy
smathy

Reputation: 27961

Your indentation suggests your seeds.rb file is inside the db/seeds directory, it should be up in db/seeds.rb

Upvotes: 0

Related Questions