Should I code something like this in view?
<%= @childrens.count %>
or
controller
@children = Parent.children
@children_count = @children.count
view
<%= @children_count%>
or should I add one column called ‘count’ in Parent table, and every time user creates new CommunityTopic record, should I re-count the number of records and save the number to ‘count ‘ column? at the same time, I’d like to update ‘at_modified(TimeStamp)’ of Parent table, when new CommunityTopic has been created.
You should execute db requests in the controller, not in the view.
In your situation, counting the children could be done like so :
And then display
@childrenin your view :By the way, check this link : http://web.archive.org/web/20100210204319/http://blog.hasmanythrough.com/2008/2/27/count-length-size It will give you additional information about when to use
count,size, orlength.