-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from abdulhamiid/processing-data-in-models
Processing data in models
- Loading branch information
Showing
8 changed files
with
94 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :author, class_name: 'User' | ||
belongs_to :post | ||
after_save :update_comments_counter | ||
|
||
def update_comments_counter | ||
post.increment!(:comments_counter) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Like < ApplicationRecord | ||
belongs_to :author, class_name: 'User' | ||
belongs_to :post | ||
after_save :update_likes_counter | ||
|
||
def update_likes_counter | ||
post.increment!(:likes_counter) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Post < ApplicationRecord | ||
belongs_to :author, class_name: 'User' | ||
has_many :likes | ||
has_many :comments | ||
after_save :update_posts_counter | ||
|
||
def update_posts_counter | ||
author.increment!(:posts_counter) | ||
end | ||
|
||
def five_recent_comments | ||
comments.order('created_at Desc').last(5) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'author_id' | ||
has_many :likes, foreign_key: 'author_id' | ||
|
||
def recent_posts | ||
posts.order('created_at Desc').last(3) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RenameColumnInPosts < ActiveRecord::Migration[7.0] | ||
def change | ||
rename_column :posts, :user_id, :author_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RenameColumnInComments < ActiveRecord::Migration[7.0] | ||
def change | ||
rename_column :comments, :user_id, :author_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RenameColumnInLikes < ActiveRecord::Migration[7.0] | ||
def change | ||
rename_column :likes, :user_id, :author_id | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.