Skip to content
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

Merged
merged 8 commits into from
Nov 30, 2023
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ To execute the tests, run the following command inside the project folder:
## πŸ”­ Future Features <a name="future-features"></a>

- [x] Creating a data model.
- [x] Processing data in models.
- [ ] Validations and Model specs.
- [ ] Processing data in models.
- [ ] Controllers.
- [ ] Controllers specs.
- [ ] Views.
Expand Down
11 changes: 11 additions & 0 deletions app/models/comment.rb
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
Comment on lines +8 to +10

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

end
11 changes: 11 additions & 0 deletions app/models/like.rb
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

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_likes_counter

end
16 changes: 16 additions & 0 deletions app/models/post.rb
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
Copy link

@Strangeal Strangeal Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Great work on defining the methods for your post model! 😊 To enhance the maintainability and organization of your code, I recommend considering the use of Rails Active Callbacks, specifically after_save, to automatically trigger the update_posts_counter method each time a new post is saved to the database. This approach will help you keep your code clean and ensure that the counter is always up to date without manual intervention.

Below is an example of making use of after_save:

after_save :update_posts_counter

def update_posts_counter
    author.increment!(:posts_counter)
end

Keep up the good work! πŸ‘πŸš€

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Strangeal! Oh, nice 🎯, thank you for the suggestion! I will totally implement it now. πŸ₯‚


def most_recent_comments(n_limit = 5)
comments.order(created_at: :desc).limit(n_limit)
end
end
9 changes: 9 additions & 0 deletions app/models/user.rb
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
2 changes: 1 addition & 1 deletion db/migrate/20231128225500_create_posts.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreatePosts < ActiveRecord::Migration[7.1]
def change
create_table :posts do |t|
t.references :author_id, null: false, foreign_key: { to_table: :users }
t.references :author, null: false, foreign_key: { to_table: :users }
t.string :title
t.text :text
t.integer :comments_counter, default: 0
Expand Down
10 changes: 5 additions & 5 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading