# Count records returned by find?

**URL:** <https://www.ruby-forum.com/t/count-records-returned-by-find/72118>\
**Category:** Rails\
**Created:** [October 14, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/count-records-returned-by-find/72118 "2006-10-14T13:20:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 14, 2006, 1:20pm UTC](https://www.ruby-forum.com/t/count-records-returned-by-find/72118/1 "2006-10-14T13:20:10Z")

</div>

I just want to create a simple “no records returned” -  
using this statement though

@positions = pq.find  
if @positions.count \< 1

generates  
“undefined method `count’ for #Array:0x4b0adc8”

I’m not looking to apply any conditions only total records returned by  
the  
find.

Anyway tell me what I’m doing wrong ?  
Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 14, 2006, 2:04pm UTC](https://www.ruby-forum.com/t/count-records-returned-by-find/72118/2 "2006-10-14T14:04:16Z")

</div>

On Oct 14, 7:18 am, “Dark A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> find.
> 
> Anyway tell me what I’m doing wrong ?  
> Stuart
> 
> –[Dark ambient - Wikipedia](http://en.wikipedia.org/wiki/Dark_ambient)

You can do…

“Nothing found” unless @positions.empty?  
or  
“Nothing found” if @positions.size == 0

\_Kevin

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 14, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/count-records-returned-by-find/72118/3 "2006-10-14T14:18:17Z")

</div>

You can also use count\_by\_sql for just counting the results. Here is  
an example:

Gift.count\_by\_sql(“select count(distinct url) from gifts”)

It can be any sql statement that returns a count().

Tom

On 10/14/06, \_Kevin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > generates  
> > You can do…
> 
> “Nothing found” unless @positions.empty?  
> or  
> “Nothing found” if @positions.size == 0
> 
> \_Kevin
> 
> >

–  
Tom D.

[http://atomgiant.com](http://atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 14, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/count-records-returned-by-find/72118/4 "2006-10-14T15:00:21Z")

</div>

On 10/14/06, \_Kevin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > –[Dark ambient - Wikipedia](http://en.wikipedia.org/wiki/Dark_ambient)
> 
> You can do…
> 
> “Nothing found” unless @positions.empty?  
> or  
> “Nothing found” if @positions.size == 0
> 
> \_Kevin

Thanks guys (Kevin and Tom)

I went with @positions.size and that works good.  
However , and this might be another thread all together I’m noticing a  
little freaky behaviour.  
This is an ajax form taking user conditions -  
what I’m finding is the first time it hits a condition that returns no  
records it won’t say no records found.  
if I go back , clear the condition and put it in again , then it will.

Sound weird ?

Stuart
