开发者

Is there a particularly elegant way to do a count query with Rails 3 / Arel?

开发者 https://www.devze.com 2023-01-29 13:40 出处:网络
H开发者_C百科ere\'s a Rails2 query: Foo.find_by_bar_and_baz(\'a-bar\', \'a-baz\', :select =>\'count(*) as the_count\' ).the_count.to_i

H开发者_C百科ere'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'" )


Foo.where(['bar = ? and baz = ?', 'a-bar', 'a-baz']).count
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号