【问题标题】:ruby active record conditions for the calculated field计算字段的 ruby​​ 活动记录条件
【发布时间】:2012-08-04 12:34:08
【问题描述】:

我正在尝试在一个查询中执行计算、对计算值应用条件并对结果进行计数。问题是我不知道如何从条件中引用计算字段。

我有 Issues.created_on 字段,我想查找有关在用户提供的几周范围内每周创建多少问题的统计信息。 这是我的查询在 MySql 中的样子:

mysql>  SELECT status_id as group_id,
YEAR(created_on)*1000+WEEK(created_on,1) as range_value, count(*) as
noOfEntries FROM `issues` WHERE (`issues`.`project_id` IN (1))  GROUP
BY issues.status_id, range_value;

+----------+-------------+-------------+
| group_id | range_value | noOfEntries |
+----------+-------------+-------------+
|        1 |     2012031 |           2 |
|        5 |     2012015 |           1 |
+----------+-------------+-------------+

这是我的代码(简化):

# Range value is dynamic: "YYYYXXX", where YYYY is year and XXX could be week, month or day of year
range_value = "YEAR(created_on)*1000+WEEK(created_on,1)"    
select = "#{range_value} as range_value, count(*) as noOfEntries"
grouping = "range_value"
# other conditions are provided by user, so we just add one here
conditions["range_value"] = range[:min]..range[:max]

rows = Issue.all(:select => select, :conditions => conditions, :readonly => true, :group => grouping)

但我收到此错误:

ActiveRecord::StatementInvalid (Mysql::Error: Unknown column 'issues.range_value' in 'where clause': SELECT YEAR(created_on)*1000+WEEK(created_on,1) as range_value, 'weeks' as range_type, count(*) as logged_hours, 1 as entries, 'priority_id' as grouping, issues.priority_id as group_id FROM `issues` WHERE (`issues`.`range_value` BETWEEN '2012012' AND '2012031' AND `issues`.`project_id` IN (1))  GROUP BY range_value, issues.priority_id ORDER BY 1 asc, 6 asc):

我要修改的代码不是我的,因此我无法向问题对象添加任何新字段或方法。所以像this 这样的解决方案对我不起作用,我猜。

【问题讨论】:

    标签: ruby activerecord count conditional-statements


    【解决方案1】:

    你应该这样做:

    rows = Issue.select(select).where(conditions).group(grouping).readonly
    

    更清楚了。

    【讨论】:

    • 好的,但顺便说一句,我应该如何处理那部分? :readonly => 真
    • 我问过,因为我只是不知道该怎么做……我见过 select()、where()、group() 之类的命令,但没有看到 readonly() 之类的 onr .那你能不能给我这行代码。
    【解决方案2】:

    我找到了一个example of code,它确实有帮助:

    ActiveRecord 没有用于在其中包含 HAVING 子句的参数 您的数据库查询,有时我发现自己需要它。幸运的是, 你可以在 :group 参数的末尾偷偷溜进去 需要求助于 find_by_sql() 调用。 (我不记得曾经使用过 HAVING 没有 GROUP BY,尽管可能在某些情况下它 这样做是有意义的。)

    例如,这是我的一个 Rails 应用程序中的一个查询, 在数据库中查找所有重复的电子邮件地址:

    duplicates = User.find( :all,
      :select     => "email, COUNT(email) AS duplicate_count",
      :conditions => "email IS NOT NULL AND email != ''",
      :group      => "email HAVING duplicate_count > 1"
    )
    

    所以稍微修改后我的代码就可以工作了:

    # Range value is dynamic: "YYYYXXX", where YYYY is year and XXX could be
    # week, month or day of year
    range_value = "YEAR(created_on)*1000+WEEK(created_on,1)"    
    select = "#{range_value} as range_value, count(*) as noOfEntries"
    # CHANGED THIS LINE from: grouping = "range_value"
    grouping = " HAVING range_value BETWEEN #{range[:min]} AND #{range[:max]}"
    
    # other conditions are provided by user, so we just add one here
    # REMOVED THIS LINE
    # conditions["range_value"] = range[:min]..range[:max]
    
    rows = Issue.all(:select => select, :conditions => conditions, :readonly => true, :group => grouping)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      相关资源
      最近更新 更多