For some reason it seems like .each block iterate only once, when I write lets say 3 or 4 comments, @total_comments displays only 1, instead of 3 or 4.
The relationships are:
- user has many posts
- post has many comments
Can anybody help please?
controller
@total_comments = 0
@posts = current_user.posts
@posts.each do |post|
@comments = post.comments
@new_comments = @comments.count -@comments.where("created_at < ?",1.day.ago).count
@total_comments += @new_comments
end
view
<%= @total_comments %>
To get the count of all comments you want something like this
Although without knowing your data model it’s difficult to say exactly.
As discussed with @m_x, a neater and more rails-3/ruby 1.9 esque version of above