0

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?

Foo
  • 800
  • 1
  • 7
  • 17

1 Answers1

1

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).

ewiinnnnn
  • 995
  • 7
  • 7
  • Thanks for the suggestion. I'll try to use counter cache. by the way, what the point of using size instead of count? – Foo Jan 05 '13 at 02:00
  • this will help you, http://stackoverflow.com/questions/6083219/activerecord-size-vs-count – ewiinnnnn Jan 05 '13 at 02:01