Failed to validate on rake db: seed

I am doing chapter 12 of a hartle tutorial. When I ran bundle exec rake db:seed , I got this error:

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

I'm trying to run

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

And finally

 rake db:populate 

but they did not 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: " 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 # 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) } 

Perhaps the problem is in this line email = "example-#{n+1}@railstutorial.org"

+5
source share
4 answers

Your problem is that rake db: reset not only reduces and recreates the database, but it also carries over seeds as well , therefore, essentially, this happens:

 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 db rake: prepare the command, your problem will disappear. However, to avoid this in the future, I highly recommend inserting some logic into your seed file. This is just Ruby, so you can wrap the User creation in an if statement, for example:

 unless User.find_by( email: " example@railstutorial.org " ) # create all 100 users end 

This will be especially valuable if you have a production site that still uses seed data (for example, the SiteSetting table); you need to make sure that the data gets into your production database, but you will create duplicate records (or errors) that start the seed, without discarding.

For additional help to answer your question, see the selected answer to this .

Hope this provides all the information you need!

+5
source

I am doing chapter 12 of a hartle tutorial. When I ran the exec rake db: seed package, I got this error:

ActiveRecord :: RecordInvalid: validation failed: email has already been received

When you run rake db:reset , it will start the database for you. When you run rake db:seed , an exception will be thrown because you are using create! in your seeds.rb file. Unlike create create! throws an exception when checks are not performed.

You can verify this by running rake db:reset and then using rails console to check the entries in the database.

There are a few things you could do to prevent this, but why would you, when your data already exists?

When I run rake db: populate, it gives:

I don't know how to build the 'db: populate' task

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

+1
source

try using:

If this is an existing email address, he will solve it.

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

and you can also see errors:

 user = User.new(name: "Example User", email: " example@railstutorial.org ", password: "foobar", password_confirmation: "foobar", admin: true, activated: true, activated_at: Time.zone.now) user.errors user.save if user.valid 
-1
source

Do you have both a faker and a populator installed in your Gemfile? This is most likely due to a problem. Make sure you 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: http://sudharti.imtqy.com/articles/using-faker-and-populator-rails/

-1
source

All Articles