# Checking for records in a database

**URL:** <https://www.ruby-forum.com/t/checking-for-records-in-a-database/131363>\
**Category:** Rails\
**Created:** [March 8, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/checking-for-records-in-a-database/131363 "2008-03-08T21:03:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 8, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/checking-for-records-in-a-database/131363/1 "2008-03-08T21:03:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 8, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/checking-for-records-in-a-database/131363/2 "2008-03-08T21:12:49Z")

</div>

Tom S. 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

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 8, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/checking-for-records-in-a-database/131363/3 "2008-03-08T21:15:04Z")

</div>

Tom S. 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

---

<div class="post-metadata">

**Author:** ![tomx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@tomx](https://www.ruby-forum.com/u/tomx)\
**Post date:** [March 8, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/checking-for-records-in-a-database/131363/4 "2008-03-08T21:26:15Z")

</div>

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