Foo
Foo

Reputation: 810

Should I prepare the 'count' column when I'm showing the number of records each parent has?

I have Parent model and Child model.
Parent has many children.

In index.html.erb, I'm showing the number of records that each parent has.
But I bet it makes laggy when many people are accessing to this page at the same time.

<% @parents.each do |parent| %>
 <td><%= parent.child.count %></td>
<% end %>

Should I create the column called 'count' in Parent table so that it won't need to calculate everytime it renders?

Upvotes: 0

Views: 37

Answers (1)

ewiinnnnn
ewiinnnnn

Reputation: 995

If this page, index.html.erb, will be often visited by your user, then I think you should make the 'count' column, maybe paired with counter cache, like this http://railscasts.com/episodes/23-counter-cache-column (sorry the video was old but it'll give you the idea).

Upvotes: 1

Related Questions