Heroku运行rake db:种子错误

时间:2016-03-02 18:50:54

标签: ruby-on-rails ruby heroku

我是在Rails 4.2.4中构建条带支付处理系统的新开发人员。

我的应用程序在本地完美运行,但当我将其推送到Heroku时,运行rake db:seed后出现此错误。

我现在无法让应用程序在生产级别打开,尽管之前它运行良好。

这是终端中的错误:

rake aborted!
    ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/validations.rb:79:in `raise_record_invalid'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/validations.rb:43:in `save!'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:291:in `block in save!'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:220:in `transaction'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:291:in `save!'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/persistence.rb:51:in `create!'
    /app/db/seeds.rb:9:in `<top (required)>'
    /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
    /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'
    /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
    /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
    /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/engine.rb:547:in `load_seed'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
    /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
    Tasks: TOP => db:seed

这是seeds.rb:

AdminUser.create!(email: 'admin@example.com', password: 'password', password_confirmation: 'password')

placenta_wash = Product.create(title: "Placenta Wash", 
 subtitle: "Nippon Beauty", author: "Chris Dorman", 
 length: "30 minutes", author_image_name: "teacher-image.jpg", 
 price: "4.99", sku: "GROHACK1", download_url: "https://s3.amazonaws.com/one-month-stripe-payments-dorman/CV.pdf", 
 details: "You'll get one video", description: %{<p>A growth hacker is a rare combination: someone with the right marketing and technical skills who can come up with clever marketing hacks and also track their results.</p>

 <p>In this talk, Mattan Griffel introduces you to the concept of Growth Hacking and shares his favorite tips for getting started as a growth hacker.</p>
 <p><strong>What You'll Learn</strong></p>
 <ul class="no-indent">
 <li>What is a growth hacker?</li>
 <li>The 5 stages of the user lifecycle</li>
 <li>How to apply the Lean Marketing Framework</li>
 <li>Resources and tools you'll need to know</li>
 </ul>}, 
 author_description: %{ <p>Hey, I'm Mattan Griffel, co-founder of the Y Combinator backed <a href="http://www.onemonth.com" target="_blank">One Month</a> where I teach the best selling One Month Rails and One Month Growth Hacking.</p>
 <p>Why take my class? In the past I've advised companies like Pepsico, Bloomberg, GM, NYSE, and JPMorgan on growth hacking best practices. I've also spoken at New York University, Cooper Union and First Round Capital. My goal is to help you and other entrepreneurs rapidly grow your business. 
 </p>
 <p>Follow me on Twitter <a href="https://twitter.com/mattangriffel" target="_blank">@mattangriffel</a></p>})

growth_hacking_monthly = Product.create(title: "Growth Hacking Monthly", 
 subtitle: "Ongoing Course", author: "Chris Dorman", 
 length: "6 months", author_image_name: "teacher-image.jpg", 
 price: "4.99", sku: "GROHACK2", download_url: "https://s3.amazonaws.com/one-month-stripe-payments/Peter+Bell+Resume.pdf", 
 details: "We'll charge you monthly for unlimited access to our mentoring team", description: %{<p>A growth hacker is a rare combination: someone with the right marketing and technical skills who can come up with clever marketing hacks and also track their results.</p>

 <p>In this series, Mattan Griffel introduces you to the concept of Growth Hacking and shares his favorite tips for getting started as a growth hacker.</p>
 <p><strong>What You'll Learn</strong></p>
 <ul class="no-indent">
 <li>What is a growth hacker?</li>
 <li>The 5 stages of the user lifecycle</li>
 <li>How to apply the Lean Marketing Framework</li>
 <li>Resources and tools you'll need to know</li>
 </ul>}, 
 author_description: %{ <p>Hey, I'm Mattan Griffel, co-founder of the Y Combinator backed <a href="http://www.onemonth.com" target="_blank">One Month</a> where I teach the best selling One Month Rails and One Month Growth Hacking.</p>
 <p>Why take my class? In the past I've advised companies like Pepsico, Bloomberg, GM, NYSE, and JPMorgan on growth hacking best practices. I've also spoken at New York University, Cooper Union and First Round Capital. My goal is to help you and other entrepreneurs rapidly grow your business. 
 </p>
 <p>Follow me on Twitter <a href="https://twitter.com/mattangriffel" target="_blank">@mattangriffel</a></p>})

1 个答案:

答案 0 :(得分:1)

您看到这一点是因为您在AdminUser模型的电子邮件字段中进行了唯一性验证。种子失败,因为您在seends.rb中创建的记录因电子邮件not being unique而无效。这个问题不是Heroku特有的 - 如果你在本地连续两次运行rake db:seed,它应该以同样的方式失败。

您可以尝试将类似AdminUser.destroy_all的内容添加到seeds.rb文件中(如果不是用户,则为模型的名称)。虽然你不想删除生产数据,但我会非常小心地非常

更好的是,只需将unless AdminUser.find_by(email: 'admin@example.com')添加到种子文件第一行的末尾。

如果您只是想在应用中添加一些数据,则可能需要查看创建填充任务。有RailsCast on this subject