Checking for records in a database

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.

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.. :slight_smile:

ilan

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

Exelent, the #count? was exactly what was needed, thanks guys! :smiley: