How to get atoms of data from the DB

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

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

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

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.
<[email protected]

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

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

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

was what I wanted to write