【问题标题】:active record joins and match on all活动记录加入并匹配所有
【发布时间】: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


【解决方案1】:
user_ids = {}.compare_by_identity
user_ids['user_id'] = [1]
user_ids['user_id'] = [4]

Posts.includes(:commentors).references(:commentors).where(commentors: user_ids)

希望这行得通!

【讨论】:

    猜你喜欢
    • 2016-09-26
    • 2016-10-11
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多