解決したいこと
Raisls tutorial14章 リスト 14.14: サンプルデータにfollowing/followerの関係性を追加した後、rails db:seedを実施したら、エラーが出てきました。それを解決したいと思っています!
エラー内容
ec2
1rails aborted! 2ActiveRecord::RecordInvalid: Validation failed: Email has already been taken 3/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/validations.rb:78:in `raise_validation_error' 4/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/validations.rb:50:in `save!' 5/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/attribute_methods/dirty.rb:43:in `save!' 6/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/transactions.rb:313:in `block in save!' 7/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status' 8/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction' 9/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction' 10/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction' 11/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction' 12/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/transactions.rb:210:in `transaction' 13/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/transactions.rb:381:in `with_transaction_returning_status' 14/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/transactions.rb:313:in `save!' 15/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/suppressor.rb:46:in `save!' 16/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/persistence.rb:51:in `create!' 17/home/ec2-user/environment/sample_app/db/seeds.rb:2:in `<top (required)>' 18/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load' 19/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load' 20/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency' 21/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load' 22/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.1.6/lib/rails/engine.rb:549:in `load_seed' 23/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks.rb:270:in `load_seed' 24/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.1.6/lib/active_record/railties/databases.rake:184:in `block (2 levels) in <top (required)>' 25/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.1.6/lib/rails/commands/rake/rake_command.rb:21:in `block in perform' 26/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.1.6/lib/rails/commands/rake/rake_command.rb:18:in `perform' 27/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.1.6/lib/rails/command.rb:46:in `invoke' 28/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (required)>' 29/home/ec2-user/environment/sample_app/bin/rails:9:in `require' 30/home/ec2-user/environment/sample_app/bin/rails:9:in `<top (required)>' 31/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load' 32/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call' 33/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call' 34/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/lib/spring/client.rb:30:in `run' 35/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/bin/spring:49:in `<top (required)>' 36/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load' 37/home/ec2-user/.rvm/gems/ruby-2.6.3/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>' 38/home/ec2-user/environment/sample_app/bin/spring:15:in `require' 39/home/ec2-user/environment/sample_app/bin/spring:15:in `<top (required)>'
解決に向けて実施したこと
Eメールが重複しているというエラーなので、調べてみたところこのような記事がありました。
https://qiita.com/suzuki-koya/items/02d5e349bff179c44454
こちらを実施してみましたが、エラーは治りませんでした。
また、heroku pg:reset DATABASEとrails db:migrate:resetを打った後rails db:seedしてもエラーは治りませんでした。
関連ファイル
app/models/user.rb
class
1 has_many :microposts, dependent: :destroy 2 has_many :active_relationships, class_name: "Relationship", 3 foreign_key: "follower_id", 4 dependent: :destroy 5 has_many :passive_relationships, class_name: "Relationship", 6 foreign_key: "followed_id", 7 dependent: :destroy 8 has_many :following, through: :active_relationships, source: :followed 9 has_many :followers, through: :passive_relationships, source: :follower 10 attr_accessor :remember_token, :activation_token, :reset_token 11 before_save :downcase_email 12 before_create :create_activation_digest 13 14 15 validates :name, presence: true, length: { maximum: 50 } 16 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 17 validates :email, presence: true, length: { maximum: 255 }, 18 format: { with: VALID_EMAIL_REGEX }, 19 uniqueness: { case_sensitive: false } 20 has_secure_password 21 validates :password, presence: true, 22 length: { minimum: 6 }, allow_nil: true 23 24 def User.digest(string) 25 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 26 BCrypt::Engine.cost 27 BCrypt::Password.create(string, cost: cost) 28 end 29 30 def User.new_token 31 SecureRandom.urlsafe_base64 32 end 33 34 def remember 35 self.remember_token = User.new_token 36 self.update_attribute(:remember_digest, 37 User.digest(remember_token)) 38 end 39 40 def forget 41 self.update_attribute(:remember_digest, nil) 42 end 43 44 # 渡されたトークンがダイジェストと一致したらtrueを返す 45 def authenticated?(attribute, token) 46 digest = self.send("#{attribute}_digest") 47 return false if digest.nil? 48 BCrypt::Password.new(digest).is_password?(token) 49 end 50 51 def activate 52 update_attribute(:activated, true) 53 update_attribute(:activated_at, Time.zone.now) 54 end 55 56 def send_activation_email 57 UserMailer.account_activation(self).deliver_now 58 end 59 60 # パスワード再設定の属性を設定する 61 def create_reset_digest 62 self.reset_token = User.new_token 63 update_attribute(:reset_digest, User.digest(reset_token)) 64 update_attribute(:reset_sent_at, Time.zone.now) 65 end 66 67 # パスワード再設定のメールを送信する 68 def send_password_reset_email 69 UserMailer.password_reset(self).deliver_now 70 end 71 72 # パスワード再設定の期限が切れている場合はtrueを返す 73 def password_reset_expired? 74 reset_sent_at < 2.hours.ago 75 end 76 77 # 試作feedの定義 78 # 完全な実装は次章の「ユーザーをフォローする」を参照 79 def feed 80 Micropost.where("user_id = ?", id) 81 end 82 83 # ユーザーをフォローする 84 def follow(other_user) 85 following << other_user 86 end 87 88 # ユーザーをフォロー解除する 89 def unfollow(other_user) 90 active_relationships.find_by(followed_id: other_user.id).destroy 91 end 92 93 # 現在のユーザーがフォローしてたらtrueを返す 94 def following?(other_user) 95 following.include?(other_user) 96 end 97 98 private 99 100 def downcase_email 101 self.email = self.email.downcase 102 end 103 104 def create_activation_digest 105 self.activation_token = User.new_token 106 self.activation_digest = User.digest(self.activation_token) 107 # @user.activation_digest => ハッシュ値 108 end 109end
app/models/relationship.rb
class
1 belongs_to :follower, class_name: "User" 2 belongs_to :followed, class_name: "User" 3 validates :follower_id, presence: true 4 validates :followed_id, presence: true 5end
使用しているツール
AWS、cloud9、rails5.1.6、ruby5.1.2、heroku、MySql
質問は以上になります。分かりにくいかもしれませんが回答よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー