如何编写Activerecord/Arel交集查询

2024-02-19

我在 PSQL 中得到了这个工作:

SELECT "profiles".id FROM "profiles"
INNER JOIN "integration_profiles"
ON "integration_profiles"."profile_id" = "profiles"."id"
INNER JOIN "integrations"
ON "integrations"."id" = "integration_profiles"."integration_id"
WHERE "integrations"."provider" = 'csv'
INTERSECT
SELECT "profiles".id FROM "profiles"
INNER JOIN "integration_profiles"
ON "integration_profiles"."profile_id" = "profiles"."id"
INNER JOIN "integrations"
ON "integrations"."id" = "integration_profiles"."integration_id"
WHERE "integrations"."provider" = 'kickstarter'

我想将其转换为 ActiveRecord 查询或 Arel。

我试过:

Arel::Nodes::Intersect.new(
  Profile.joins(:integrations).where(integrations: { provider: 'csv' }),
  Profile.joins(:integrations).where(integrations: { provider: 'kickstarter' })
)

[Debug]   Profile Load (408.2ms)  SELECT "profiles".* FROM "profiles" INNER JOIN "integration_profiles" ON "integration_profiles"."profile_id" = "profiles"."id" INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" WHERE "integrations"."provider" = 'csv' (pid:65570)
 [Debug]   Profile Load (1.9ms)  SELECT "profiles".* FROM "profiles" INNER JOIN "integration_profiles" ON "integration_profiles"."profile_id" = "profiles"."id" INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" WHERE "integrations"."provider" = 'kickstarter' (pid:65570)

这有效:

ps = (
  Profile.select(:id)
    .joins(:integrations)
    .where(integrations: { provider: 'csv' })
    .intersect(
      Profile.select(:id)
        .joins(:integrations)
        .where(integrations: {provider: 'kickstarter'})
    )
)
Profile.from(Profile.arel_table.create_table_alias(ps, :profiles))

有没有一种方法可以一步完成?或者一种获取方法ps输出结果,它只是输出一个 Arel 对象......


您可以加入 2 次integration_profiles 和集成,如下所示:

Profile
  .joins("INNER JOIN integration_profiles AS ip_csv ON ip_csv.profile_id = profiles.id")
  .joins("INNER JOIN integrations AS i_csv ON i_csv.id = ip_csv.integration_id")
  .joins("INNER JOIN integration_profiles AS ip_kickstarter ON ip_kickstarter.profile_id = profiles.id")
  .joins("INNER JOIN integrations AS i_kickstarter ON i_kickstarter.id = ip_kickstarter.integration_id")
  .where("i_csv.provider='csv' AND i_kickstarter.provider='kickstarter'")
  .pluck("profiles.id")

如果您想要一个更具可读性的查询,请定义两个范围,如下所示:

class Profile < ActiveRecord::Base
  has_many :integration_profiles
  has_many :integrations, through: :integration_profiles

  scope :csv,         ->{joins(:integrations).where(integrations: { provider: 'csv' })}
  scope :kickstarter, ->{joins(:integrations).where(integrations: { provider: 'kickstarter' })}
end

进而:

Profile.csv.where(id: Profile.kickstarter.pluck(:id) ).pluck(:id)

您最终会得到 2 个查询,但这读起来要好得多。

SELECT "profiles"."id" FROM "profiles"
INNER JOIN "integration_profiles" ON "integration_profiles"."profile_id" = "profiles"."id" 
INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" 
WHERE "integrations"."provider" = 'kickstarter'

SELECT "profiles"."id" FROM "profiles"
INNER JOIN "integration_profiles" ON "integration_profiles"."profile_id" = "profiles"."id" 
INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" 
WHERE "integrations"."provider" = 'csv' AND "profiles"."id" IN (1, 3)

如果您只需要配置文件的 ID,您可以采用另一条路径:从integration_profiles 中提取 profile_id:

class Integration < ActiveRecord::Base
  has_many :integration_profiles
  has_many :profiles, through: :integration_profiles

  scope :csv,         -> { where(provider: 'csv') }
  scope :kickstarter, -> { where(provider: 'kickstarter') }
end

class IntegrationProfile < ActiveRecord::Base
  belongs_to :integration
  belongs_to :profile
end

IntegrationProfile.joins(:integration).csv.where(
  profile_id: IntegrationProfile.joins(:integration).kickstarter.pluck(:profile_id)
).pluck(:profile_id)

这仍然会生成 2 个查询,但这些查询更简单:

SELECT "integration_profiles"."profile_id" FROM "integration_profiles"
INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" 
WHERE "integrations"."provider" = 'kickstarter'

SELECT "integration_profiles"."profile_id" FROM "integration_profiles"
INNER JOIN "integrations" ON "integrations"."id" = "integration_profiles"."integration_id" 
WHERE "integrations"."provider" = 'csv' AND "integration_profiles"."profile_id" IN (1, 3)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何编写Activerecord/Arel交集查询 的相关文章

  • Delayed_job不会运行用户定义的方法

    我一直在尝试让 DelayedJob 在后台运行一些用户定义的方法 对于这个测试用例 我在助手中定义了以下方法 def test case u User new u first name JimBob u last name joe u e
  • 在Rails 3中,如何使用button_to更改布尔值?

    我正在尝试编写一个button to 语句来更新我的activerecord 数据库中的布尔值 这是我尝试过的 从更大的角度来看 我想做的是有一个按钮可以更新支付对象并触发对宝石汇款与亚马逊支付进行通信的私有方法的调用 所以 1 如何使用b
  • 部署到 Heroku 时出现“rake 中止!堆栈级别太深”

    我的网站曾经运行正常 Heroku 预编译了资产和所有内容 现在 似乎不知从何而来 我开始在部署时收到此消息 Preparing app for Rails asset pipeline Running rake assets precom
  • :autosave 忽略了 has_many 关系——我错过了什么?

    我有两门课 class Collection lt ActiveRecord Base has many items autosave true end class Item lt ActiveRecord Base belongs to
  • Rails 4 - 如何在活动记录查询中为includes()和joins()指定别名

    我如何给出别名 例如includes 给出如下 用户 活动记录模型 Student 活动记录模型 继承自User STI Teacher 活动记录模型 继承自User STI 项目 活动记录模型 这里有一些例子 第一个案例 更多性传播感染协
  • Doctrine 在 Active Record 之上添加了什么 - CodeIgniter?

    我真的很喜欢 CodeIgniter Active Record 它非常好地允许我进行所有需要的数据库查询 但我也一直在阅读有关 ORM 的内容 例如 Doctrine 当我阅读 Doctrine 的文档时 它的使用似乎不像 Active
  • 在 Heroku 上的生产模式下使用 Rails 同步 gem 与 Faye 和 Thin

    我正在尝试设置 同步 gem 以在我的 Rails 应用程序中启用实时更新 它使用 Faye 作为实时推送服务 并使用 Thin 作为网络服务器 我对此非常陌生 所以任何建议都会受到赞赏 我的本地服务器上有这个工作 但不知道如何让它在 He
  • 我应该在 Heroku 上部署 Ruby on Rails 应用程序吗?

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 关于我自己的一些情况 我今年 24 岁 去年从北卡罗来纳州立大学毕业 获得了分析硕士学位 统计 数学之类的 我没有很强的编程背景 这对我的问题非
  • 带有 JavaScript 函数的 Submit_tag

    我是rails新手 我想在单击提交按钮时调用javascript函数 我使用了submit tag 但该函数没有被触发 我想要如下所示的东西 我用谷歌搜索了这个问题 但找不到解决方案 请任何人提供解决方案 尝试单击 或者 如果您想使用 on
  • 嵌套表单的解决方案

    我已经被这个问题困扰了一段时间 需要制作一个表格比赛 category与自定义输入 它应该取所有值信息表并构建输入 但棘手的部分是它应该保存到类别 信息 table class Competition lt ApplicationRecor
  • Rails:控制文件存储缓存大小

    Rails 中基于文件的缓存的文档说 请注意 缓存将不断增长 直到磁盘已满 除非您 定期清除旧条目 不幸的是 它没有提供任何有关如何定期清除旧条目的信息 是否设置合适的值 expires in完成这项工作还是清除缓存背后还有其他某种黑魔法
  • Rails3-jquery-autocomplete 如果没有可用数据则清除字段

    我正在使用rails3 jquery autocomplete 如果自动完成没有返回结果 是否可以清除自动完成字段的值 我试图阻止用户提交数据库中没有的值 我使用的是旧版本 但我实现了change处理程序autocomplete call
  • 为 has_many 或 habtm 动态创建 after_add 和 after_remove 回调?

    有没有办法动态添加after add and after remove回调现有的has many or has and belongs to many关系 例如 假设我有模型User Thing 和一个连接模型UserThingRelati
  • Fields_for 动态标签

    我有动态表单 它有一组值 我创建了一个包含我显示的文本字段的部分 在每个文本旁边 我想显示一个包含文本标题的标签 例如 名字和姓氏以前是未知的 我该如何去做呢 看来我无法直接访问属性 但是当我使用标签字段时 标签中的变量名称显示而不是实际值
  • “after_filter”如何/何时工作/运行?

    我正在使用 Ruby on Rails 3 1 我想知道 对于表现原因 无论是否after filter渲染视图文件后运行 也就是说 当用户访问我的应用程序 URL 时 他 她应该显示的相关视图文件先于渲染after filter运行或af
  • 在 Rails 中获取实际的远程 IP?

    我正在开发 Rails 2 3 8 应用程序 我需要存储实际客户端的远程 IP 地址 request remote ip仅返回127 0 0 1 但我需要存储实际的远程IP 例如93 43 56 77 有红宝石可以买吗 或者如何得到它 尝试
  • 如何从字符串中提取包含非英文字符的 URL?

    下面是一个简单的脚本 它采用其中包含德语 URL 的锚标记 并提取 URL encoding utf 8 require uri url URI extract a href M nchen a puts url http www exam
  • Rails 模型中 self 的价值是什么?为什么没有明显的实例方法可用?

    我的 Rails 3 1 6 应用程序中有一个自定义访问器方法 即使该值不存在 它也会为属性分配一个值 my attr 属性是一个序列化哈希 除非指定了空白值 否则应将其与给定值合并 在这种情况下 它将把当前值设置为空白值 添加了检查以确保
  • 从 JSON 在 Devise 中创建用户

    我正在致力于将运行 Devise 的 Rails 3 1 应用程序与我的 iOS 应用程序集成以进行用户身份验证 我希望用户能够从应用程序注册 然后我可以存储这些凭据以便稍后登录 使用 RestKit 我这样做 IBAction regis
  • Rails 2 Mailer View 将 3D 添加到字符串之前

    我有一个非常旧的 Rails 应用程序 它试图为新用户发送验证电子邮件 但永远找不到令牌 因为由于某种原因 无论我如何生成链接 链接都会以 3D 形式添加到字符串前面 由于某种原因 它似乎还在标记的中间注入了一个 符号 这是一些带有输出的代

随机推荐