0

I am running through Hartl's Rails tutorial, but when I try to seed Microposts I am getting the error: "ActiveRecord::RecordInvalid: Validation failed: Email has already been taken"

I did db:migrate:reset followed by db:seed which throws up the error. Is there something wrong with what I am trying to seed?

User.create!(name: "Example User",
             email: "example@railstutorial.org",
             password: "foobar",
             password_confirmation: "foobar",
             admin: true,
             activated: true,
             activated_at: Time.zone.now)

99.times do |n|
  name= Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name: name,
               email: email,
               password: password,
               password_confirmation: password,
               activated: true,
               activated_at: Time.zone.now)
end

  users = User.order(:created_at).take(6)
  50.times do
    content = Faker::Lorem.sentence(word_count: 5)
    users.each { |user| user.microposts.create!(content: content) }
  end
SJK
  • 135
  • 1
  • 10
  • 1
    Since you're using Faker, consider [Faker::Internet.unique.email](https://www.rubydoc.info/gems/faker/Faker/Internet#uuid-class_method). I don't see anything wrong, there must be an existing User. Check the database is empty before you seed; run the console and check `User.all`. – Schwern Oct 19 '20 at 07:34
  • Can you show us your User class, please? – Schwern Oct 19 '20 at 07:37
  • If I do User.all I come up with two users, my Example User and then one more, so it is not seeding the users along with the microposts. – SJK Oct 19 '20 at 09:46
  • Are you clearing your users table between runs? – Nick M Oct 19 '20 at 11:30

4 Answers4

1

You have probably already created some records in the database, which invokes validation. To check this, run the console rails c and then type User.first.

If you have some records run rake db:reset to reset database.

If you want to be able to run the seed multiple times, write

User.find_or_create_by(email: email) do |u|
  u.name: name,
  u.password: password,
  u.password_confirmation: password,
  u.activated: true,
  u.activated_at: Time.zone.now
end

instead your code.

krzyskk
  • 90
  • 1
  • 8
0

Instead of create! user create so if the email address is already taken, the record would not be created and the script gracefully continues, and you won't get an exception.

Loqman
  • 1,487
  • 1
  • 12
  • 24
  • 1
    I would recommend sticking with `create!` since sometimes it would be more useful to see the error than having some of the seed items skipped – msuliq Dec 16 '22 at 06:19
0

I'm not sure what exactly happened but I went ahead and reset my migrations once more and when I reseeded it worked. So if anyone else has this problem in the future, just clear the database again and try again.

SJK
  • 135
  • 1
  • 10
0

In my case I had a syntax error when calling Rake :: ..... It worked for me to update the gem with gem 'faker', '~> 2.15', '> = 2.15.1' Then rails db: migrate: reset and rails db: seed.