mysql - 将mysql查询转换为rails查询
问题描述
大家好我在将mysql查询转换为rails查询时遇到问题。我有这些模型-
class User < ApplicationRecord
has_many :comments, foreign_key: "commenter_id"
end
class Comment < ApplicationRecord
belongs_to :commenter, class_name: "User"
end
谁能帮我将以下查询转换为rails查询-
UPDATE comments
INNER JOIN users on comments.commenter_id = users.id
SET comments.deleted_at = users.deleted_at
WHERE users.deleted_at IS NOT NULL
我正在尝试对其评论者被软删除的软删除评论。
更新1:到目前为止,我可以通过使用这个来做到这一点-
User.only_deleted.includes(:comments).find_each do |u|
u.comments.update_all(deleted_at: u.deleted_at)
end
但我想在单个查询上执行此操作,而不必遍历结果。
更新2:我正在使用acts_as_paranoid gem,因此需要取消范围的用户,我的最终查询变为:
User.unscoped{Comment.joins(:commenter).where.not(users: {deleted_at: nil}).update_all("comments.deleted_at = users.deleted_at")
解决方案
这应该适用于 MySQL:
Comment
.joins(:user)
.where.not(users: { deleted_at: nil })
.update_all("comments.deleted_at = users.deleted_at")
这在 Postgres 上不起作用,因为它缺少用户的 FROM 子句。
一个性能较差但多语言的选项是:
Comment
.joins(:user)
.where.not(users: { deleted_at: nil })
.update_all("deleted_at = ( SELECT users.deleted_at FROM users WHERE comments.id = users.id )")
这仍然可能比遍历 Ruby 中的记录好一个数量级,因为您消除了应用服务器和数据库之间的流量延迟。
推荐阅读
- http-post - “Promise”类型上不存在属性“id”
' - amazon-web-services - 如何从 serverless.yml 中的多个 yml 文件中加入或合并 provider.patterns 数组,以及如何在预定事件上设置启用标志
- python - python - 如何通过python aiogram中的机器人在同步函数中发送消息?
- azure-functions - 设置设置 SCM_DO_BUILD_DURING_DEPLOYMENT=true 后,我得到 write failed No space left on device (28) 错误
- quantization - Lloyd max 算法的零概率条件对连续情况有何意义?
- python - 合并熊猫数据框,合并非关键的同一列
- php - 目标类控制器在本地主机中不存在 Laravel 8 一切正常但不在线
- linux - 过去的网络连接是否存储在某处?
- google-sheets - google sheet:如何仅授予对工作表中特定范围的写入权限?
- xamarin - Rider: MSB1003 msb1003 指定项目或解决方案文件