是否有一种特别优雅的方式来使用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