Rails 范围 - 完全匹配的地方 [英] Rails scope - where in exact matches
本文介绍了Rails 范围 - 完全匹配的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 where IN (?)
查询在 Rails 中创建范围,这将检查完全匹配?
Is it possible to make scope in Rails with where IN (?)
query, which will check exact matches?
例如:
Post.joins(:tags).where('tags.id IN (?)', [1, 2, 3, 4])
将查找带有标签 1, 2
, 1, 2, 3
和 1, 2, 3, 4
的帖子.但应该只找到带有 1, 2, 3, 4
标签的帖子.
will find posts with tags 1, 2
, 1, 2, 3
and 1, 2, 3, 4
. But should find only post with 1, 2, 3, 4
tags.
推荐答案
获得匹配 IN
子句中所有值的想法你必须这样做:
The idea to get matching all values in IN
clause you have to do this:
tag_ids = [1, 2, 3, 4]
Post.joins(:tags).where('tags.id IN (?)', tags_ids).group("posts.id")
.having("COUNT(posts.id) >= ?", tag_ids.length)
希望对你有帮助.
这篇关于Rails 范围 - 完全匹配的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文