假设我有以下模型:
User
has_many :posts
Post
belongs_to :user
我在数据库中有以下数据:
User(id: 1)
User(id: 2)
Post(id: 1, user_id: 1)
Post(id: 2, user_id: 1)
Post(id: 3, user_id: 1)
我想构建一个返回用户并按照以下标准匹配的搜索搜索:
它应该返回:User(id:1),因为他符合所有条件。
我尝试使用“高级模式”http://ransack-demo.herokuapp.com/users/advanced_search 并且可以使用两个条件定义分组条件,但它仅使用一个连接操作生成SQL:
SELECT DISTINCT "users".* FROM "users"
LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
WHERE ((("posts"."id" IN (1, 2))
AND ("posts"."id" IN (3, 4, 5, 6))))
我期待这样的事情:
SELECT DISTINCT "users".* FROM "users"
LEFT OUTER JOIN "posts" "posts_X" ON "posts_X"."user_id" = "users"."id"
LEFT OUTER JOIN "posts" "posts_Y" ON "posts_Y"."user_id" = "users"."id"
WHERE ((("posts_X"."id" IN (1, 2))
AND ("posts_Y"."id" IN (3, 4, 5, 6))))
是否有可能通过搜索来实现它?