【问题标题】:Rails - how to join two relation based on single View?Rails - 如何基于单个视图加入两个关系?
【发布时间】:2014-10-12 12:25:15
【问题描述】:

我在我的 Rails 应用程序中定义了一个视图,遵循guide

所以,目前我有这个课程:

class GoldenResult < ActiveRecord::Base
    scope :latest_builds, -> { select('MAX(id) as id').group(:platform_id, :release_id, :configuration_id) }

end

我想执行以下连接:

SELECT * FROM performance_dev.report_golden_results e 
join (SELECT max(id) as 'id' 
from performance_dev.report_golden_results 
group by platform_id, release_id, configuration_id) s 
ON s.id = e.id;

问题是我不知道如何访问 ON 连接变量(即ON s.id = e.id

创建此查询的常规方法是什么?

谢谢!

【问题讨论】:

标签: mysql ruby-on-rails activerecord ruby-on-rails-4


【解决方案1】:

我最终通过使用以下查询解决了这个问题:

GoldenResult.joins("INNER JOIN (SELECT MAX(id) as 'id'
                                FROM report_golden_results
                                GROUP BY platform_id, release_id, configuration_id) latest_builds
                    ON latest_builds.id=report_golden_results.id")

这是一个解决方法,因为我仍然不知道如何访问 JOIN 中的范围,但它可以工作..!

(即 - 在这个解决方案中,我根本没有使用范围)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2019-11-10
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多