Rails 与 children 的数组连接查询:获取对应的 parents 与 "ALL" children,而不是 "ANY"
Rails join query with array of children : get corresponding parents with "ALL" children, not "ANY"
我有这个 ActiveRecord 查询,它为我提供了包含 tag_ids 中列出的任何标签的帖子:
Post.joins(:tags).where(tags: { id: tag_ids })
有什么我可以变得排外而不是包容的吗?
我只想要匹配 tag_ids.
中所有标签的帖子
我正在使用 Postgres,并希望通过 arel
来实现?
谢谢!
试试这个:
Post.joins(:tags).where(tags: { id: tag_ids}).group(:id).having("COUNT(DISTINCT tags.id) = #{tag_ids.count}")
我有这个 ActiveRecord 查询,它为我提供了包含 tag_ids 中列出的任何标签的帖子:
Post.joins(:tags).where(tags: { id: tag_ids })
有什么我可以变得排外而不是包容的吗? 我只想要匹配 tag_ids.
中所有标签的帖子我正在使用 Postgres,并希望通过 arel
来实现?
谢谢!
试试这个:
Post.joins(:tags).where(tags: { id: tag_ids}).group(:id).having("COUNT(DISTINCT tags.id) = #{tag_ids.count}")