-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Milestone 2: models associations #2
Changes from all commits
7f79928
273e10f
597ed7a
3a3e39a
776f430
5b07b92
d10a8ec
691166a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :author, class_name: 'User', foreign_key: :user_id | ||
belongs_to :post, class_name: 'Post', foreign_key: :post_id | ||
|
||
# * Rails ActiveRecord callbacks to update counters in User model after saving a Comment | ||
after_save :update_comments_counter | ||
|
||
def update_comments_counter | ||
post.increment!(:comments_counter) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Like < ApplicationRecord | ||
belongs_to :author, class_name: 'User', foreign_key: :user_id | ||
belongs_to :post, class_name: 'Post', foreign_key: :post_id | ||
|
||
# * Rails ActiveRecord callbacks to update counters in User model after saving a Like | ||
after_save :update_likes_counter | ||
|
||
def update_likes_counter | ||
post.increment!(:likes_counter) | ||
end | ||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hint : Just like I mentioned in my second comment, |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Post < ApplicationRecord | ||
belongs_to :author, class_name: 'User', foreign_key: :author_id | ||
has_many :comments, class_name: 'Comment', foreign_key: :post_id, dependent: :destroy | ||
has_many :likes, class_name: 'Like', foreign_key: :post_id, dependent: :destroy | ||
|
||
# * Rails ActiveRecord callbacks to update counters in User model after saving a Post. | ||
after_save :update_posts_counter | ||
|
||
def update_posts_counter | ||
author.increment!(:posts_counter) | ||
end | ||
Comment on lines
+8
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Below is an example of making use of
|
||
|
||
def most_recent_comments(n_limit = 5) | ||
comments.order(created_at: :desc).limit(n_limit) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class User < ApplicationRecord | ||
has_many :posts, class_name: 'Post', foreign_key: :author_id | ||
has_many :comments, class_name: 'Comment', foreign_key: :user_id | ||
has_many :likes, class_name: 'Like', foreign_key: :user_id | ||
|
||
def most_recent_posts(n_limit = 3) | ||
posts.order(created_at: :desc).limit(n_limit) | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hint : Just like I mentioned in my second comment,
after_save :update_comments_counter