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: #processing data in models #2

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
- [💻 Getting Started ](#-getting-started-)
- [Prerequisites](#prerequisites)
- [Setup](#setup)
- [🚀 Presentation Video](#Video)
- [Install](#install)
- [Usage](#usage)
- [👥 Author ](#-author-)
Expand Down Expand Up @@ -49,6 +48,7 @@ Blog-app is a fully functioning website that show the list of posts and empower

- **Create Database named Blog_app_development**
- **Create tables by migration files**
- **processing data in models**

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down Expand Up @@ -107,7 +107,6 @@ it will install the required gemfile for running the project

## 🔭 Future Features <a name="future-features"></a>
- Validations and Model specs.
- Processing data in models.
- Controllers.
- Controllers specs.
- Views.
Expand Down
8 changes: 8 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user

def update_post_comments_counter
post.update(comments_counter: post.comments.count)
end
end
8 changes: 8 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Like < ApplicationRecord
belongs_to :post
belongs_to :user

def update_post_likes_counter
post.update(likes_counter: post.likes.count)
end
end
13 changes: 13 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, foreign_key: 'post_id'
has_many :likes, foreign_key: 'post_id'

def update_user_posts_counter
author.update(posts_counter: author.posts.count)
end

def recent_comments(limit = 5)
comments.order(created_at: :desc).limit(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, foreign_key: 'author_id'
has_many :comments, foreign_key: 'user_id'
has_many :likes, foreign_key: 'user_id'

def recent_posts(limit = 3)
posts.order(created_at: :desc).limit(limit)
end
end
Loading