加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用Rails 3 / Arel进行计数查询是否有一种特

发布时间:2020-12-17 03:09:55 所属栏目:百科 来源:网络整理
导读:这是一个Rails2查询: Foo.find_by_bar_and_baz('a-bar','a-baz',:select ='count(*) as the_count' ).the_count.to_i 查询很难看,但最终的SQL是理想的. 在Rails 3 / Arel中有更优雅的方法吗? 编辑 这更漂亮但仍然没有Arel魔法: Foo.count( :conditions =
这是一个Rails2查询:

Foo.find_by_bar_and_baz('a-bar','a-baz',:select =>'count(*) as the_count' ).the_count.to_i

查询很难看,但最终的SQL是理想的.

在Rails 3 / Arel中有更优雅的方法吗?

编辑

这更漂亮但仍然没有Arel魔法:

Foo.count( :conditions => "bar = 'a-bar' and baz = 'a-baz'" )

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读