From d3ec59dd2e565c362f438adddcc638b74faf0642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Geonizeli?= Date: Sun, 27 Feb 2022 17:26:22 -0300 Subject: [PATCH] fix post specs --- app/models/post.rb | 8 ++++---- spec/requests/posts_spec.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 3157d1a..17d9af6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -28,10 +28,10 @@ class Post < ApplicationRecord end def limit_of_post_per_day - posts_from_day = user.posts.where('created_at >= ?', Time.zone.now.beginning_of_day) + posts_from_day = user&.posts&.where('created_at >= ?', Time.zone.now.beginning_of_day) - if posts_from_day.count >= 5 - errors.add(:base, 'You can post only 5 posts per day') - end + return if posts_from_day && posts_from_day.count < 5 + + errors.add(:base, 'You can post only 5 posts per day') end end diff --git a/spec/requests/posts_spec.rb b/spec/requests/posts_spec.rb index da2237d..c76ef02 100644 --- a/spec/requests/posts_spec.rb +++ b/spec/requests/posts_spec.rb @@ -24,7 +24,7 @@ RSpec.describe "/posts", type: :request do let(:invalid_attributes) { { - content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum." + content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum." } }