ruby-on-rails - 如何在rails中组合两个joins.where
问题描述
帮助在一个大、好或语法上加入查询,以便每个公司都有点应用程序,其中
这是 2 个单独工作的查询字符串。
DotApplication.all.joins(:current_stage_event).where(current_stage_event: {event: "prospect"}).joins('LEFT JOIN tasks on tasks.subject_id = dot_applications.id').where(tasks: {id: nil})
Company.all.joins(:users).where(users: { notification_for_active_prospects: true})
我想要的是:
Company.each do |company|
# For each company(with joins,where above), find all dot_applications as in the examples above
end
Company和DotApplication之间的关联如下所示:
# app/models/company.rb
class Comany < ApplicationRecord
has_many :dot_aplications
end
# app/models/dot_application.rb
class DotApplication < ApplicationRecord
belongs_to :company
end
解决方案
Company.joins(:dot_applications)
.where(dot_appplications: DotApplication.joins(:current_stage_event)
.where(current_stage_event: {event: "prospect"})
.joins('LEFT JOIN tasks on tasks.subject_id = dot_applications.id')
.where(tasks: {id: nil}))
.joins(:users)
.where(users: { notification_for_active_prospects: true})
这应该找到所有满足您要求的公司DotApplication
,然后查询这些公司的User
要求
推荐阅读
- java - Activity 如何从后台服务获取最新更新?
- javascript - CSS长宽比和-webkit-aspect-ratio不起作用
- tensorflow - TensorFlow Keras 预测 one-hot-vectors
- azure - 使用 terraform 将 Azure rbac 角色分配给虚拟机
- vue.js - 挂载函数多次调用vue.js
- r - as.POSIXct() 对某些日期产生“格式不明确”错误,但对其他日期则没有
- java - 我不断收到 java.lang.NoClassDefFoundError: org/junit/platform/engine/ConfigurationParameters with Eclipse,但安装了 Junit5
- laravel - 具有多个身份验证提供程序和守卫的多租户应用程序
- reactjs - 如何在 Ant Design List Item 中添加 onClick 事件?
- python - 用另一个 DataFrame 查询一个 DataFrame