【发布时间】:2021-02-11 23:12:34
【问题描述】:
您好,我需要更新此查询,从匹配所有 4 和 1 以包含用户 4 和用户 1 对同一帖子发表评论的所有帖子
这是我的错误活动记录查询
Posts.joins(:commentors).where(commentors: {user_id: ["","4", "1"]})
【问题讨论】:
-
你可以急切地加载关系。
Posts.includes(:commentors).references(:commentors).where(commentors: {user_id: [...]}). -
谢谢,但返回的所有内容包括“1 或 4”而不是“1 和 4”
-
你可以在 where 子句中使用原始 sql。
where('commentors.user_id IN (?)', [...])
标签: ruby-on-rails activerecord