前提・実現したいこと
現在、作成したオリジナルアプリをhttps://qiita.com/naoki_mochizuki/items/5a1757d222806cbe0cd1こちらの記事に従ってawsにデプロイしようとしています。こちらの記事はMysqlを前提としていたので、https://note.com/itoa06/n/n31fe4f9cd6b9こちらを参考に変更しました。
変更後、rcpecを行うとすべて通っていたはずが通らなくなりました。
解決方法わかる方いましたら教えて頂きたいです。
また、下記のエラーはrspecとし、全体をテストした時に起こります。 rspec spec/requests等とするとエラーが起きません。なぜでしょうか?
発生している問題・エラーメッセージ
Failures: 1) User is posts from unfollowed users are not displayed Failure/Error: FactoryBot.create(:post, :post_image, user: other_user) ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/models/user_spec.rb:94:in `block (2 levels) in <main>' 2) Comments #create as an authenticated user responds successfully Failure/Error: let(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/comments_request_spec.rb:8:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' 3) Comments #create as an guest user is can't comment on a post Failure/Error: let(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/comments_request_spec.rb:8:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' 4) Comments #destroy as other user is can't delete comment Failure/Error: let(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' # ./spec/requests/comments_request_spec.rb:8:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/comments_request_spec.rb:7:in `block (2 levels) in <main>' 5) Like #create as an authenticated user responds successfully Failure/Error: let!(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/like_request_spec.rb:6:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/like_request_spec.rb:6:in `block (2 levels) in <main>' 6) Like #create as an authenticated user is can like posts Failure/Error: let!(:post_image) { FactoryBot.create(:post, :post_image, user: user) } ActiveRecord::InvalidForeignKey: Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) # ./spec/requests/like_request_spec.rb:6:in `block (2 levels) in <main>' # ------------------ # --- Caused by: --- # Mysql2::Error: # Cannot add or update a child row: a foreign key constraint fails (`viewhome_test`.`posts`, CONSTRAINT `fk_rails_5b5ddfd518` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) ...まだ続きます
spec/models/user_spec.rb
require 'rails_helper' RSpec.describe User, type: :model do let(:user) { FactoryBot.create(:user) } let(:other_user) { FactoryBot.create(:user, email: "other_user@example.com") } . . . # フォローしていないユーザーの投稿は表示されない it "is posts from unfollowed users are not displayed" do FactoryBot.create(:other_user) FactoryBot.create(:post, :post_image, user: other_user) other_user.posts.each do |unfollowed| expect(user.feed).to_not include(unfollowed) end end end
###app/models/user.rb
class User < ApplicationRecord has_one_attached :icon has_many :likes, dependent: :destroy has_many :liked_posts, through: :likes, source: :user has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships attr_accessor :remember_token, :activation_token, :reset_token before_save :downcase_email before_create :create_activation_digest validates :name, presence: true, length: { maximum:15 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i validates :email, presence: true, length: {maximum:255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true ...
###app/models/post.rb
class Post < ApplicationRecord belongs_to :user has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user has_many :comments, dependent: :destroy has_one_attached :image default_scope -> { order(created_at: :desc) } validates :user_id, presence: true validates :name, presence: true, length: { maximum:50 } validates :content, presence: true, length: { maximum:140 } validates :image, presence:true, content_type: { in: %w[image/jpeg image/gif image/png], message: "有効な画像形式である必要があります" }, size: { less_than: 5.megabytes, message: "5MB未満である必要があります" } def display_image image.variant(resize_to_fill:[280,600]) end def like_by?(user) likes.where(user_id: user_id).exist? end def self.sort_like Post.all.sort{|a,b| b.liked_users.count <=> a.liked_users.count} end end
補足情報(FW/ツールのバージョンなど)
Rails 6.0.3.2
ruby 2.7.1
ubuntu 18.04 LTS
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/01 05:08
2020/11/01 05:25
2020/11/01 06:15
2020/11/01 06:17
2020/11/01 09:01
2020/11/01 09:05
2020/11/01 11:57
2020/11/01 11:58
2020/11/02 15:19
2020/11/04 15:32
2020/11/04 23:22
2020/11/05 00:52
2020/11/05 01:04
2020/11/05 07:48
2020/11/05 08:18
2020/11/05 11:23
2020/11/05 12:43
2020/11/05 13:11
2020/11/05 22:57
2020/11/06 00:49
2020/11/06 01:13
2020/11/06 02:30
2020/11/06 02:34
2020/11/06 05:36
2020/11/06 06:02
2020/11/06 06:11
2020/11/06 06:18
2020/11/06 06:26
2020/11/06 06:28
2020/11/06 06:40
2020/11/06 09:08
2020/11/07 01:45