更新合并
Cama::PostType.first.posts.joins(:custom_field_values)
.where("cama_custom_fields_relationships.custom_field_slug = ? AND
cama_custom_fields_relationships.value LIKE ?","localization",
"%Paris%").merge(Cama::PostType.first.posts.joins(:custom_field_values)
.where("cama_custom_fields_relationships.custom_field_slug = ? AND
cama_custom_fields_relationships.value = ?","type-localization", "2"))
这也不起作用。当执行分离时,它返回相同的AssociationRelation。我猜它只适用于ActiveRecord:Relation
更新 我想我正在寻找INTERSECT,但不知道如何在哪里使用它
我创建了另一个主题,但仍然找不到如何优化它的答案。
就像这样
我需要通过“ other_model”值查找“帖子”。其他模型与另一个表中的帖子有关系,但让它保持简单。当我这样做
Foo.joins(:other_model).where("other_model.value = ? AND other_model.value = ?", "one", "two")
这当然不会找到任何结果,因为它与自己矛盾。
当我使用OR而不是AND时
Foo.joins(:other_model).where("other_model.value = ? OR other_model.value = ?", "one", "two")
它为我找到了帖子,但是...要么具有一个值,要么具有第二个值,并且...
我想查找基于other_model.value = 1和other_model.value = 2的帖子
这意味着它将寻找2个单独的结果,然后我只需要返回相互覆盖的ID ...有意义吗?
答案 0 :(得分:1)
我认为您正在寻找类似的查询
Foo
.joins(:other_model)
.where('other_model.value = ? OR other_model.value = ?', 'one', 'two')
.group('foos.id')
.having('COUNT(other_models.id) >= 2')