diff --git a/README.md b/README.md index f066cf6..3eeb429 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ - [💻 Getting Started ](#-getting-started-) - [Prerequisites](#prerequisites) - [Setup](#setup) - - [🚀 Presentation Video](#Video) - [Install](#install) - [Usage](#usage) - [👥 Author ](#-author-) @@ -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**

(back to top)

@@ -107,7 +107,6 @@ it will install the required gemfile for running the project ## 🔭 Future Features - Validations and Model specs. - - Processing data in models. - Controllers. - Controllers specs. - Views. diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..099f191 --- /dev/null +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..33193b3 --- /dev/null +++ b/app/models/like.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..359579c --- /dev/null +++ b/app/models/post.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..66cd914 --- /dev/null +++ b/app/models/user.rb @@ -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