ruby-on-rails - 如何在 Rails 中为自定义 SQL 查询创建动态 where 子句?

标签 ruby-on-rails postgresql activerecord

我们需要用双分组依据进行计数,据我所知,Rails 2.3 中的 ActiveRecord 不支持它,因此我们正在执行自定义 SQL 查询,例如:

ActiveRecord::Base.connection.execute("select COUNT(*), author_id, call_type_id from signup_calls group by author_id, call_type_id")

但是,我们还需要过滤由三个用户选择的过滤器计算的注册调用,这需要包含在 where 子句中。

有没有什么方法可以使用 ActiveRecord 助手来帮助生成 where 子句。如果不是,最好的方法是什么?

最佳答案

你可以做到

SignupCall.select('count(*) as signup_count, author_id, call_type_id').group('author_id, call_type_id')

然后在后面链接 where 子句。

关于ruby-on-rails - 如何在 Rails 中为自定义 SQL 查询创建动态 where 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11037705/

相关文章:

mysql - Mac OS X Lion - mysql : command not found

ruby-on-rails - 使用 each_with_index 循环时如何将索引合并到变量名中?

ruby-on-rails - has_many :through, 缓存、touch 和 counter_cache 的问题

php - 无法加载请求的文件: home. php

ruby-on-rails - rails : Order model object by attribute of referenced model object

ruby-on-rails - 为什么 Rails 中的语言环境设置是全局的(使用 Thin 时)?

ruby-on-rails - 如何在Peatio中添加比特币?

java - PSQLException 仅 8.2 及更高版本的服务器支持返回自动生成的键

node.js - 如何使用knex alertableBuilder更新列注释

postgresql - 为什么开放街道 map (OSM) 使用 PostgreSQL 数据库?