5

I'm doing chapter 12 of hartle's tutorial. When I ran bundle exec rake db:seed I got this error:

ActiveRecord::RecordInvalid: Validation failed: Email has already been taken

I try running

rake db:reset
rake db:migrate
rake db:test:prepare

And at last

rake db:populate 

but they didn't solve the problem. When I run rake db:populate it gives:

Don't know how to build task 'db:populate'

This is my seeds.rb file:

    # Users
User.create!(name:  "Example User",
             email: "[email protected]",
             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

# Microposts
users = User.order(:created_at).take(6)
50.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.microposts.create!(content: content) }
end

# Following relationships
users = User.all
user  = users.first
following = users[2..50]
followers = users[3..40]
following.each { |followed| user.follow(followed) }
followers.each { |follower| follower.follow(user) }

I guess maybe the problem is with this line email = "example-#{n+1}@railstutorial.org"

1
  • I dont see a problem with your seeds file. Are you sure the db dropped properly. Maybe go inside rails c and check if there is anytging persisted. Or try catching the exception and identifying which record is causing the problem.
    – user2536065
    May 1, 2015 at 12:25

4 Answers 4

5

Your problem is that rake db:reset not only drops and recreates the database, but it also migrates and seeds it as well. So essentially what's happening is this:

rake db:drop
rake db:create
rake db:schema:load # (think of this as running all the migrations you've run before)
rake db:seed # (creates your 100 database users)

and then you run:

rake db:migrate # (likely unnecessary, but it causes no harm)
rake db:test:prepare # (prepares the test database)
rake db:prepare # (runs the seeds AGAIN and causes your errors)

Obviously, from this if you just stop running the rake db:prepare command your problem will go away. However, to avoid these things in the future, I strongly recommend putting a little bit of logic in your seed file. It's just Ruby, so you could wrap the User creates in an unless statement, such as:

unless User.find_by( email: "[email protected]" )
  # create all 100 users
end

This will prove to be especially valuable if you have a site on production that still uses seed data (such as a SiteSetting table); you need to make sure the data makes its way into your production database, but you'll create duplicate records (or errors) running the seed again without dropping.

As an additional reference for the answer to your question, see the selected answer to this one.

I hope this provides all the information you need!

9
  • Thanks for your answer...I run the commands you mentioned without rake db:prepare but still the error remains...I'm so confused, really don't know what to do!
    – Hanna
    May 1, 2015 at 13:45
  • Just for clarity, you were running four commands at the beginning ( rake db:reset, rake db:migrate, rake db:test:prepare, rake db:prepare ) and getting an error on the last one, correct? If so, simply don't run the last one. The error you are getting is because the last command is redundant. May 1, 2015 at 14:15
  • No I didn't run 'rake db:prepare'
    – Hanna
    May 1, 2015 at 17:00
  • Then what exactly are you running when the error manifests itself now? rake db:prepare, rake db:reset, rake db:seed, and bundle exec rake db:Seed are all running your seeds. Using more than one will always give you this error. May 1, 2015 at 17:24
  • 1
    Ah, I understand. You are correct, there's no problem. All Mr. Hartl wants you to do in that passage is put the seeds into your database, and you have successfully done so. May 1, 2015 at 18:59
1

I'm doing chapter 12 of hartle's tutorial. When I ran bundle exec rake db:seed I got this error:

ActiveRecord::RecordInvalid: Validation failed: Email has already been taken

When you run rake db:reset, it will seed the database for you. When you then run rake db:seed, an exception will be thrown, because you are using create! in your seeds.rb file. Unlike create, create! raises an exception when the validations fail.

You can check this by running rake db:reset, and then using rails console to check your database entries.

There are a couple things you could do to prevent this, but why would you, when your data is already there?

When I run rake db:populate it gives:

Don't know how to build task 'db:populate'

Unless you define it yourself, there is no rake task named db:populate.

-1

try using:

if the case is already existing email it will solve it.

email = "example-#{rand(100000)}@railstutorial.org"

and you can also see errors:

user = User.new(name:  "Example User",
         email: "[email protected]",
         password:              "foobar",
         password_confirmation: "foobar",
         admin:     true,
         activated: true,
         activated_at: Time.zone.now)
user.errors
user.save if user.valid
5
  • The same thing 'Validation failed: Email has already been taken'
    – Hanna
    May 1, 2015 at 12:30
  • go to console and see output of User.all.pluck(:email).sort and see for email id's if exist between [email protected]@railstutorial.org
    – Abdul Baig
    May 1, 2015 at 12:31
  • One of them is "[email protected]" which was defined at first and others are between [email protected] and [email protected]
    – Hanna
    May 1, 2015 at 12:35
  • and you have validation on user email. so User.delete_all then rake db:seed or rake db:drop db:create db:migrate then rake db:seed should work
    – Abdul Baig
    May 1, 2015 at 12:39
  • I deleted all users as you said and ran rake db:drop rake db:create , rake db:migrate , rake db:seed...but after that when I run bundle exec rake db:seed I see this again:(and in User.all.pluck(:email).sort I can see all those emails again!) rake aborted! ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
    – Hanna
    May 1, 2015 at 13:02
-1

Do you have both faker and populator installed in your Gemfile? That is most likely apart of the issue. Make sure you have run:

gem install populator #From the command line

and include it in your Gemfile:

gem 'populator'

Here is a link to the Git repo https://github.com/ryanb/populator/tree/master

Great article here also: http://sudharti.github.io/articles/using-faker-and-populator-rails/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.