Is there a particularly elegant way to do a count query with Rails 3 / Arel?
-
25-09-2019 - |
Question
Here's a Rails2 query:
Foo.find_by_bar_and_baz('a-bar', 'a-baz', :select =>'count(*) as the_count' ).the_count.to_i
The query is ugly, but the resulting SQL is ideal.
Is there a more elegant way to do this in Rails 3 / Arel?
edit
this is prettier but still no Arel magic:
Foo.count( :conditions => "bar = 'a-bar' and baz = 'a-baz'" )
Solution
Foo.where(['bar = ? and baz = ?', 'a-bar', 'a-baz']).count
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow