Rails 3 / Arelでカウントクエリを行う特にエレガントな方法はありますか?
-
25-09-2019 - |
質問
これがRails2クエリです:
Foo.find_by_bar_and_baz('a-bar', 'a-baz', :select =>'count(*) as the_count' ).the_count.to_i
クエリは醜いですが、結果のSQLが理想的です。
Rails 3 / Arelでこれを行うためのよりエレガントな方法はありますか?
編集
これはきれいですが、それでもアレルの魔法はありません:
Foo.count( :conditions => "bar = 'a-bar' and baz = 'a-baz'" )
解決
Foo.where(['bar = ? and baz = ?', 'a-bar', 'a-baz']).count
所属していません StackOverflow