这是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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top