bundle exec rake db:populate - activerecord :: recordinvalid:验证失败:已经收到电子邮件

时间:2014-07-03 08:47:09

标签: ruby-on-rails ruby activerecord railstutorial.org

我目前正处于michael hartl教程的第10章,当我尝试捆绑exec rake db:populate时出现问题。

附加的是错误消息:

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)    

附件是sample_data.rake文件:

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    admin = User.create!(name: "Example User",
                         email: "example@railstutorial.org",
                         password: "foobar",
                         password_confirmation: "foobar",
                         admin: true)
    User.create!(name: "Example User",
                 email: "example@railstutorial.org",
                 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

2 个答案:

答案 0 :(得分:3)

admin = User.create!(name: "Example User",
  email: "example@railstutorial.org",
  password: "foobar",
  password_confirmation: "foobar",
  admin: true)

User.create!(name: "Example User",
  email: "example@railstutorial.org", # This email is used to create admin above.
                                      # Use another email here
  password: "foobar",
  password_confirmation: "foobar")

错误来自此行

users = User.all(limit: 6)

活动记录方法all不接受任何参数。将其更改为

users = User.limit(6)

答案 1 :(得分:0)

最有可能的是,您已经解决了问题,我也遇到了这个问题并通过在模型文件中添加attr_accessible :content或其他attr_accessible来解决问题。我希望这能帮助别人。