2010-12-09 24 views
4

Voici une requête Rails2:Existe-t-il une manière particulièrement élégante de faire une requête de compte avec Rails 3/Arel?

Foo.find_by_bar_and_baz('a-bar', 'a-baz', :select =>'count(*) as the_count').the_count.to_i 

La requête est laid, mais le SQL résultant est idéal.

Existe-t-il une manière plus élégante de faire cela dans Rails 3/Arel?

modifier

c'est plus joli mais toujours pas de magie Arel:

Foo.count(:conditions => "bar = 'a-bar' and baz = 'a-baz'") 

Répondre

7
Foo.where(['bar = ? and baz = ?', 'a-bar', 'a-baz']).count 
+1

yay! ou encore plus mignon: 'Foo.where (: bar => 'a-bar',: baz => 'a-baz') .count' –

+3

ou sans le tableau:' Foo.where ('bar =? et baz = ? ',' a-bar ',' a-baz '). count' – iain

+1

A Rails 3, exemple concret: 'Job.where (is_active: 1, is_approved: 1) .count' – rxgx