Hi, I am have recently followed a tutorial for a basic blog system using Rails (version 2.0.2). In order to get used to the way to develop Rails apps, I decided to improve on what the tutorial showed me. However I have gotten caught on a snag, I have a Blogposts controller and a Comments model which is working, (comments are displayed and added properly) but I want my application to say something along the lines of "No comments added" if there are no comments added. I have tried using the .exists? method which is probably the way to do it but I can''t figure it out. If anyone can help, assistance will be much appreciated. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tom Savage wrote:> Hi, I am have recently followed a tutorial for a basic blog system > using Rails (version 2.0.2). In order to get used to the way to > develop Rails apps, I decided to improve on what the tutorial showed > me. > > However I have gotten caught on a snag, I have a Blogposts controller > and a Comments model which is working, (comments are displayed and > added properly) but I want my application to say something along the > lines of "No comments added" if there are no comments added. > > I have tried using the .exists? method which is probably the way to do > it but I can''t figure it out. > > If anyone can help, assistance will be much appreciated.If your comments belong to a post model then to check the amount of comments for a post, it would be something along the lines of: post_instance.comments.blank? If you are just looking for the amount of comments in the db, then it''s Comment.count hth hope you are enjoying ActiveRecord.. it is a pretty amazing OR package.. :) ilan -- 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 -~----------~----~----~----~------~----~------~--~---
Tom Savage wrote:> added properly) but I want my application to say something along the > lines of "No comments added" if there are no comments added. > > I have tried using the .exists? method which is probably the way to do > it but I can''t figure it out.The #exists? method checks to see if a particular entry is there, rather than if any entry is there. Try #count instead: @post.comments.count == 0 -- 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 -~----------~----~----~----~------~----~------~--~---
Exelent, the #count? was exactly what was needed, thanks guys! :D --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---