# How to get atoms of data from the DB

**URL:** <https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502>\
**Category:** Rails\
**Created:** [October 6, 2007, 6:43am UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502 "2007-10-06T06:43:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eeby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eeby](https://www.ruby-forum.com/u/eeby)\
**Post date:** [October 6, 2007, 6:43am UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502/1 "2007-10-06T06:43:13Z")

</div>

I’d like to get single values from the DB. For example…

SELECT COUNT(\*) freaks WHERE status=‘freaky’;

I’m not sure how to approach that in Rails. Seems like ActiveRecord is  
all about getting an object or array of objects.

Of course I could just…

Freak.find(:all …conditions…)

and then get the length of the array. But that seems wasteful. Maybe  
not. Is that what people normally do?

Any help would be appreciated.

Thanks,

Eeby

---

<div class="post-metadata">

**Author:** ![eeby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eeby](https://www.ruby-forum.com/u/eeby)\
**Post date:** [October 6, 2007, 7:30am UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502/2 "2007-10-06T07:30:42Z")

</div>

Is count\_by\_sql what you want?

Freak.count\_by\_sql("SELECT COUNT(\*) FROM freaks WHERE status=‘freaky’)

Jarrod M. wrote:

> I’d like to get single values from the DB. For example…
> 
> SELECT COUNT(\*) freaks WHERE status=‘freaky’;
> 
> I’m not sure how to approach that in Rails. Seems like ActiveRecord is  
> all about getting an object or array of objects.
> 
> Of course I could just…
> 
> Freak.find(:all …conditions…)
> 
> and then get the length of the array. But that seems wasteful. Maybe  
> not. Is that what people normally do?
> 
> Any help would be appreciated.
> 
> Thanks,
> 
> Eeby

---

<div class="post-metadata">

**Author:** ![eeby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eeby](https://www.ruby-forum.com/u/eeby)\
**Post date:** [October 6, 2007, 7:51am UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502/3 "2007-10-06T07:51:28Z")

</div>

Wes Rogers wrote:

> Is count\_by\_sql what you want?
> 
> Freak.count\_by\_sql("SELECT COUNT(\*) FROM freaks WHERE status=‘freaky’)

Thanks, it look as though it might be. I’ll try it.

Eeby

---

<div class="post-metadata">

**Author:** ![eeby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eeby](https://www.ruby-forum.com/u/eeby)\
**Post date:** [October 6, 2007, 8:29am UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502/4 "2007-10-06T08:29:35Z")

</div>

U can do freak.findbystatus and pass in freaky as status

Sent from my iPhone

On Oct 5, 2007, at 9:43 PM, Jarrod M.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![eeby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eeby](https://www.ruby-forum.com/u/eeby)\
**Post date:** [October 6, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502/5 "2007-10-06T18:41:23Z")

</div>

Freak.count(:conditions=\>[:status=\>‘freaky’]) should do it

more generic - but more sql-dialect-specific

Freak.connection.select\_one("SELECT count(\*) FROM freaks WHERE  
status=‘freaky’ ")

should give you atomic precision.

P

---

<div class="post-metadata">

**Author:** ![eeby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ecb155/32.png) [@eeby](https://www.ruby-forum.com/u/eeby)\
**Post date:** [October 6, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/how-to-get-atoms-of-data-from-the-db/116502/6 "2007-10-06T18:44:18Z")

</div>

Why has google groups no 'Ruby-syntax-check ?

Freak.count(:conditions =\> { :status=\>‘freaky’ } )

was what I wanted to write
