Rails scope - where in exact matches

前端 未结 2 1492
天涯浪人
天涯浪人 2020-12-19 19:30

Is it possible to make scope in Rails with where IN (?) query, which will check exact matches?

for example:

Post.joins(:tags).where(\'ta         


        
2条回答
  •  半阙折子戏
    2020-12-19 19:55

    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)
    

    I hope this help you.

提交回复
热议问题