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!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 There''s a bunch of documentation on the parameters and conditions count can accept. A. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 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. > > 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!! > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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''). > > ChristophHi, 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung 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 %> > > FredYes, thank you, that was the solution I was looking for. I don''t know why I insisted on the helper. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---