# Counting comments for individual threads

**URL:** <https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801>\
**Category:** Rails\
**Created:** [November 13, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801 "2006-11-13T17:15:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/1 "2006-11-13T17:15:51Z")

</div>

I’m working on a simple cookbook app, and users can leave comments. I’d  
like to show the number of comments for each recipe in the list of  
recipes. So far I can only get the count to show the total number of  
comments in the comments database; I can’t figure out how to get it to  
read the recipe\_id column in the comments field so it only spits out the  
number of comments that relates to each recipe\_id.

Here’s what I have so far:

recipes\_controller.rb:

def comment\_count  
Comment.count  
end  
helper\_method :comment\_count

list.rhtml

\<% for recipe in @recipes %\>  
\<%= recipe.title %\>  
\<%= comment\_count %\>  
\<% end %\>

Thanks!!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/2 "2006-11-13T17:30:24Z")

</div>

Dave A. wrote:

> I’m working on a simple cookbook app, and users can leave comments. I’d  
> like to show the number of comments for each recipe in the list of  
> recipes. So far I can only get the count to show the total number of  
> comments in the comments database; I can’t figure out how to get it to  
> read the recipe\_id column in the comments field so it only spits out the  
> number of comments that relates to each recipe\_id.

See  
[http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html)

There’s a bunch of documentation on the parameters and conditions count  
can accept.

A.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/3 "2006-11-13T17:31:56Z")

</div>

recipe.comments.count

You can pass additonal conditions into that, so you could for example do

recipe.comments.count :conditions =\> [‘validated = ?’, true]

To only show comments that have been validated, if you had a system  
where comments were not visible until approved.

Fred

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/4 "2006-11-13T17:47:27Z")

</div>

def comment\_count  
Comment.count(:conditions =\> "recipe\_id = " + self.id.to\_s)  
end

Might not be the best solution, but it should work (you can read about  
that in the API reference, just search for ‘count’ and  
‘ActiveRecord::Calculations’).

Christoph

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/5 "2006-11-13T19:06:06Z")

</div>

Kitto wrote:

> def comment\_count  
> Comment.count(:conditions =\> "recipe\_id = " + self.id.to\_s)  
> end
> 
> Might not be the best solution, but it should work (you can read about  
> that in the API reference, just search for ‘count’ and  
> ‘ActiveRecord::Calculations’).
> 
> Christoph

Hi, I tried this solution, and I got an error that said:

Mysql::Error: #42S22Unknown column ‘conditionsrecipe\_id’

It seems like it’s combining :conditions and recipe\_id when it’s  
SELECTing from the database. Is there a weird syntax thing going on?

I tried the solution above:

recipe.comments.count

but I got an error: undefined local variable or method `recipe’

I tried it with Recipe capitalized and got: undefined method `comments’

I know that Recipes and Comments are wired together correctly, because  
when I post a comment it enters the correct recipe\_id. I just don’t  
know why this simple counting thing doesn’t work. The resources about  
count were helpful, though, and I guess I’ll keep trying things.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/6 "2006-11-13T20:40:55Z")

</div>

Frederick C. wrote:

> > I tried the solution above:
> > 
> > recipe.comments.count
> > 
> > but I got an error: undefined local variable or method `recipe’
> 
> well you just didn’t have a recipe variable available at the time. If  
> you still sticking this in your comment\_count helper then that makes  
> perfect sense, as you are not supplying it with a recipe object.
> 
> personally I wouldn’t bother with the helper and would just have
> 
> \<% for recipe in @recipes %\>  
> \<%= recipe.title %\>  
> \<%= recipe.comments.count %\>  
> \<% end %\>
> 
> Fred

Yes, thank you, that was the solution I was looking for. I don’t know  
why I insisted on the helper.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 13, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/counting-comments-for-individual-threads/75801/7 "2006-11-13T19:51:09Z")

</div>

> I tried the solution above:
> 
> recipe.comments.count
> 
> but I got an error: undefined local variable or method `recipe’

well you just didn’t have a recipe variable available at the time. If  
you still sticking this in your comment\_count helper then that makes  
perfect sense, as you are not supplying it with a recipe object.

personally I wouldn’t bother with the helper and would just have

\<% for recipe in @recipes %\>  
\<%= recipe.title %\>  
\<%= recipe.comments.count %\>  
\<% end %\>

Fred
