Hanzawa Naoki
Hanzawa Naoki

Reputation: 503

bundle exec rake db:populate - activerecord::recordinvalid:validation failed:email has already been taken

I am currently at chapter 10 of michael hartl's tutorial and there is a problem when i try to do bundle exec rake db:populate.

Attached is the error message:

rake aborted!                                                                                                                        
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken                                                         
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/validations.rb:57:in `save!'                                  
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/attribute_methods/dirty.rb:29:in `save!'                      
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:273:in `block in save!'                       
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_st
atus'                                                                                                                                
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `bl
ock in transaction'                                                                                                                  
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `wi
thin_new_transaction'                                                                                                                
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `tr
ansaction'                                                                                                                           
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:208:in `transaction'                          
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'    
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/transactions.rb:273:in `save!'                                
/home/action/.gem/ruby/2.0.0/gems/activerecord-4.1.1/lib/active_record/validations.rb:41:in `create!'                                
/home/action/workspace/app3/lib/tasks/sample_data.rake:4:in `block (2 levels) in <top (required)>'                                   
Tasks: TOP => db:populate                                                                                                            
(See full trace by running task with --trace)    

Attached is the sample_data.rake file:

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    admin = User.create!(name: "Example User",
                         email: "[email protected]",
                         password: "foobar",
                         password_confirmation: "foobar",
                         admin: true)
    User.create!(name: "Example User",
                 email: "[email protected]",
                 password: "foobar",
                 password_confirmation: "foobar")
    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)
      users = User.all(limit: 6)
      50.times do
        content = Faker::Lorem.sentence(5)
        users.each { |user| user.microposts.create!(content: content) }
      end
    end
  end
end

Upvotes: 0

Views: 1332

Answers (2)

Shine
Shine

Reputation: 101

Most likely, you have solved the problem, I encountered this as well and solved it by adding attr_accessible :content or other attr_accessible in the model files. I hope this would help someone else.

Upvotes: 0

Santhosh
Santhosh

Reputation: 29174

admin = User.create!(name: "Example User",
  email: "[email protected]",
  password: "foobar",
  password_confirmation: "foobar",
  admin: true)

User.create!(name: "Example User",
  email: "[email protected]", # This email is used to create admin above.
                                      # Use another email here
  password: "foobar",
  password_confirmation: "foobar")

The error is from this line

users = User.all(limit: 6)

The active record method all doesnt accept any parameters. Change it to

users = User.limit(6)

Upvotes: 3

Related Questions