我在第9章的ruby on rails教程,我在最後一分鐘遇到問題。一切正常在我的網頁上,但不是在heroku上。 下面是他說的步驟:ActiveRecord驗證失敗在Heroku上運行的種子
$ git push heroku
$ heroku pg:reset DATABASE
$ heroku run rake db:migrate
$ heroku run rake db:seed
$ heroku restart
當我運行rake db:seed
Running rake db:seed on radiant-peak-3596... up, run.3507
ActiveRecord::SchemaMigration Load (2.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
(4.6ms) BEGIN
User Exists (8.1ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(3.8ms) ROLLBACK
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/validations.rb:79:in `raise_record_invalid'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/validations.rb:43:in `save!'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:291:in `block in save!'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:220:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/transactions.rb:291:in `save!'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/persistence.rb:51:in `create!'
/app/db/seeds.rb:9:in `<top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/engine.rb:547:in `load_seed'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
seed.rb
User.create!(name: "example user",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar",
admin: true)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@aol.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
end
我已經安裝了寶石,它運行完美,但Heroku的是另一種案件。
謝謝!
你的seed.rb文件是什麼樣子的?你真的使用Faker作爲你的種子世代的一部分嗎?看起來像你正在對用戶example @ hotmail.com進行硬編碼。似乎不像faker會產生什麼 –
@LeoCorrea嗨!是的,它硬編碼!它看起來像這樣。 讓我編輯 –
我假設你的數據沒有被重置,或者你正在運行'rake db:seed'兩次。這就是你的種子失敗的原因,因爲你試圖創建硬編碼的用戶兩次,驗證失敗。 –