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
Merged

Conversation

ITurres
Copy link
Owner

@ITurres ITurres commented Nov 30, 2023

🚩I have made the following changes to complete Milestone 2

Here is a summary of what has been done

Type of files changed/added

My Skills

In this new Branch, I have βž•ADDEDβž• the succeeding *πŸ“‚ folders and/or **πŸ“„ files to complete this milestone requirements

*πŸ“‚ root/

  • *πŸ“‚ app/

    - I set up all the associations between the models plus each model's custom methods.
    
    • **πŸ“„ user.rb βž•

      • A method that returns the 3 most recent posts for a given user. This method will also accept an arbitrary number of posts to return, if none is specified, it will default to 3.
    • **πŸ“„ post.rb βž•

      • A method that updates the posts counter for a user.
      • A method which returns the 5 most recent comments for a given post. This method will also accept an arbitrary number of comments to return, if none is specified, it will default to 5.
    • **πŸ“„ comment.rb βž•

      • A method that updates the comments counter for a post.
    • **πŸ“„ like.rb βž•

      • A method that updates the likes counter for a post.

I have ✏️MODIFIED✏️ the succeeding *πŸ“‚ folders and/or πŸ“„** files

*πŸ“‚ root/

  • **πŸ“„ README.md βž•

  • *πŸ“‚ db/

    • **πŸ“„ schema.rb βž•

      • A new schema.rb has been regenerated based on the migration changes.
    • *πŸ“‚ migrate/

      • **πŸ“„ 20231128225500_create_posts.rb βž•

        • I removed the _id on :author reference at CreatePosts migration for consistency.
      • **πŸ“„ 20231128225646_create_comments.rb βž•

        • I moved the t.references :post, null: false, foreign_key: true before :user but lately I have set it as it was (after ':user' - see commit history). Keeping their positions consistency as elsewhere.

Thank you for taking the time to review this PR ⭐


If you need additional information or have any questions, please don't hesitate to contact me on Slack as Arturo (Arthur) Emanuel Guerra Iturres. I'll be happy to help you out. 🎯


@ITurres ITurres self-assigned this Nov 30, 2023
@ITurres ITurres added documentation Improvements or additions to documentation enhancement New feature or request labels Nov 30, 2023
Copy link

@Strangeal Strangeal left a comment

Choose a reason for hiding this comment

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

Hi @ITurres,

Good job so far!
There are some issues that you still need to work on to go to the next project but you are almost there!

Highlights

  • Good PR title βœ…
  • Descriptive PR description βœ…
  • Implement the features βœ…
  • Nice UI design βœ…
  • Professional README template βœ…
  • Correct GitHub workflow βœ…
  • Meaningful Github commits βœ…
  • Correct .github/workflows for linters βœ…
  • Followed best-practices βœ…
  • Well structured project βœ…

Required Changes ♻️

Check the comments under the review.

Optional suggestions

Every comment with the [OPTIONAL] prefix is not crucial enough to stop the approval of this PR. However, I strongly recommend you to take them into account as they can make your code better.

Cheers and Happy coding!πŸ‘πŸ‘πŸ‘

Feel free to leave any questions or comments in the PR thread if something is not 100% clear.
Please, remember to tag me in your question so I can receive the notification.

Please, do not open a new Pull Request for re-reviews. You should use the same Pull Request submitted for the first review, either valid or invalid unless it is requested otherwise.


As described in the Code reviews limits policy you have a limited number of reviews per project (check the exact number in your Dashboard). If you think that the code review was not fair, you can request a second opinion using this form.

@@ -0,0 +1,13 @@
class Post < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: :author_id_id

Choose a reason for hiding this comment

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

  • Great job so far! πŸ‘ You're on the right track with setting up the associations in your models. However, there are a couple of improvements and corrections needed. Let's dive into the review:

    • In Rails, when you define associations like has_many or belongs_to, Rails automatically assumes the foreign key based on naming conventions unless you explicitly specify it. So, you don't need to add _id in the foreign key names when using the typical Rails naming conventions.

Instead of πŸ‘‡πŸ‘‡

   belongs_to :author, class_name: 'User', foreign_key: :author_id_id

Do this πŸ‘‡πŸ‘‡

 belongs_to :author, class_name: 'User', foreign_key: :author_id

Happy coding!

Comment on lines +5 to +8

def update_posts_counter
author.increment!(:posts_counter)
end
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. πŸ₯‚

Comment on lines +5 to +7
def update_comments_counter
post.increment!(:comments_counter)
end

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

Comment on lines +5 to +7
def update_likes_counter
post.increment!(:likes_counter)
end

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

@ITurres
Copy link
Owner Author

ITurres commented Nov 30, 2023

πŸ”΄Note: after_save Rails ActiveRecord callback has also been added to Post model. See commit d10a8ec

Copy link

@Shedrack-Sunday Shedrack-Sunday left a comment

Choose a reason for hiding this comment

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

Hi @ITurres πŸ‘
Your project is complete! There is nothing else to say other than... it's time to merge it :shipit:
Congratulations! πŸŽ‰

image

Highlights

  • Good project organization βœ”οΈ
  • Nice use of Rails features βœ”οΈ
  • Kudus on meeting the requirements as pointed the last time out by the last reviewer πŸ‘

Optional suggestions
Every comment with the [OPTIONAL] prefix won't stop the approval of this PR. However, I strongly recommend you take them into account as they can make your code better. Some of them were simply missed by the previous reviewer and addressing them will really improve your application.

Feel free to leave any questions or comments in the PR thread if something is not 100% clear.
Please, remember to tag me in your question so I can receive the notification.

As described in the Code reviews limits policy you have a limited number of reviews per project (check the exact number in your Dashboard). If you think that the code review was not fair, you can request a second opinion using this form.

@ITurres
Copy link
Owner Author

ITurres commented Nov 30, 2023

Hey @Shedrack-Sunday! Cheers for approving the PR πŸ₯‚, @Strangeal comments were very helpful. Wish you both a great week! and happy coding πŸ‘¨πŸΌβ€πŸ’».

@ITurres ITurres merged commit b71f964 into development Nov 30, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants