Domanda

Ecco una query Rails2:

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

La query è brutta, ma il SQL risultante è l'ideale.

C'è un modo più elegante per farlo in Rails 3 / Arel?

modificare

Questo è più bello ma ancora nessuna magia di Arel:

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

Soluzione

Foo.where(['bar = ? and baz = ?', 'a-bar', 'a-baz']).count
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top