Project Enhancement
When developing a project, you must always think of way to satisfy the users need and make it easier for them to use your system. In this article we will work on improving the post and comment views.
Table of contents
Comments Count
We will start this article by adding a display of how many comments have been created in a Post. To achieve this feature, we will use rails active records association option counter_cache
.
Counter Cache
Counter cache is an integer database column with the value maintained by the Rails framework for any parent model with an associated child model. This column is read_me_only, and has a predetermined number of associated children.
Setup counter_cache: true
as option for your association of comments in post. Rails will keep the cache value up to date with this declaration and then return that value in response to the size
method.
# app/models/comment.rb
class Comment < ApplicationRecord
- belongs_to :post
+ belongs_to :post, counter_cache: true
validates :content, presence: true
end
Even though the counter_cache
option is specified on the model containing the belongs_to
declaration, the actual column must be added to the associated (has_many
) model. You would also need to add a column to the Post model called comments_count
.
root@0122:/usr/src/app# rails g migration AddCommentsCountToPosts comments_count:integer
invoke active_record
create db/migrate/xxxxxxxxxxxxxx_add_comments_count_to_posts.rb
root@0122:/usr/src/app# rails db:migrate
== xxxxxxxxxxxxxx AddCommentsCountToPosts: migrating ==========================
-- add_column(:posts, :comments_count, :integer)
-> 0.0359s
== xxxxxxxxxxxxxx AddCommentsCountToPosts: migrated (0.0362s) =================
If you have an existing comments, you need to reset its counter. On your rails console, do the following:
irb(main):001:0> Post.all.each {|post| Post.reset_counters(post.id, :comments)}
Post Load (0.5ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`deleted_at` IS NULL
...
This will update all the comments count. Now you can display the comments_count
on your posts page.
<!-- app/views/posts/index.html.erb -->
<!-- ... -->
<td>content</td>
<td>category</td>
<td>user</td>
+ <td>comments count</td>
<td>action</td>
</thead>
<% @posts.each do |post| %>
<!-- ... -->
<td><%= post.content %></td>
<td><%= post.categories.pluck(:name).join(',') %></td>
<td><%= post.user&.email %></td>
+ <td><%= post.comments_count %></td>
<!-- ... -->
Seed
What exactly is Seed? Seed is a convenient method for populating a database with the initial data required for a Rails project. This allows us to populate the database in our Rails Web application.
For our seed, we will need to make a fake data, and a useful way of generating fake data is Faker
.
Read Faker article for installation and usage guide.
Pagination
On our web project, you may see many rows of information, such as a list of posts. In that case, having all of the posts listed on the same page isn’t very useful and does not look good for the users perspective. We use pagination for cases like this, which allows us to divide the rows across multiple pages and order them based on a date, name, or other field.
We will use gem Kaminari
for our pagination. Read Kaminari article for installation and usage guide.
CSV Export
The most common and useful feature in a web application is report generation. For our project we will talk about how to export our posts records into csv.
Read File Export article for installation and usage guide.
Image Upload
Our project will also have a feature where user can upload an image for their post. For image uplaoding, we will use the gem CarrierWave
.
Read Carrierwave article for installation and usage guide.