Fix #5335: Queries with "ordfav:<username>" and geometry attributes (e.g. "ratio:", "height:") crashes the api/site.

Fix `Relation passed to #and must be structurally compatible. Incompatible values: [:joins] (ArgumentError)`
exception in `ordfav:evazion ratio:4:3` search. Broken by e849d8f1c.

We were effectively doing this:

    q1 = Post.joins(:favorites, :media_asset).where("favorites.user_id = ?", 52664).order("favorites.id DESC")
    q2 = Post.joins(:media_asset, :favorites).where("ROUND(media_assets.image_width::numeric / media_assets.image_height::numeric, 2) = 1.33")
    q3 = q1.and(q2)

This failed because Rails didn't like the fact that the joins were in a different order when the
queries were `and`-ed together.
This commit is contained in:
evazion
2022-11-06 21:08:55 -06:00
parent c133866cb7
commit 174c8e0067
2 changed files with 19 additions and 2 deletions

View File

@@ -244,12 +244,12 @@ class PostQueryBuilder
in :and
joins = children.flat_map(&:joins_values)
orders = children.flat_map(&:order_values)
nodes = children.map { |child| child.joins(joins).order(orders) }
nodes = children.map { |child| child.except(:joins).joins(joins).order(orders) }
nodes.reduce(&:and)
in :or
joins = children.flat_map(&:joins_values)
orders = children.flat_map(&:order_values)
nodes = children.map { |child| child.joins(joins).order(orders) }
nodes = children.map { |child| child.except(:joins).joins(joins).order(orders) }
nodes.reduce(&:or)
end
end