Skip to content

Commit 67ac65a

Browse files
committed
Add ORM dependencies and after_destroy counter updates
1 parent 2cbe75f commit 67ac65a

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

app/models/comment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Comment < ApplicationRecord
33
belongs_to :post
44

55
after_save :update_comments_counter
6+
after_destroy :update_comments_counter
67

78
def update_comments_counter
89
post.update(comments_counter: post.comments.count)

app/models/like.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class Like < ApplicationRecord
33
belongs_to :post
44

55
after_save :update_likes_counter
6+
after_destroy :update_likes_counter
67

78
validates :author, uniqueness: { scope: :post }
89

app/models/post.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class Post < ApplicationRecord
22
belongs_to :author, class_name: 'User', foreign_key: :author_id
3-
has_many :likes
4-
has_many :comments
3+
has_many :likes, dependent: :destroy
4+
has_many :comments, dependent: :destroy
55

66
after_save :update_posts_counter
7+
after_destroy :update_posts_counter
78

89
validates :title, presence: true
910
validates :title, length: { maximum: 250 }

0 commit comments

Comments
 (0)