1
0
Fork 0
forked from OpenNeo/impress

Oops, fix not_occupies logic error

Right, previously we were querying "has *at least one asset* that is not in zone X" instead of "has NO assets that are in zone X".

I don't know a fast way to query for that, this will have to do for now!
This commit is contained in:
Matchu 2023-07-26 13:18:19 -07:00
parent 5c119abcd8
commit ab015db461
2 changed files with 8 additions and 5 deletions

View file

@ -82,14 +82,16 @@ class Item < ActiveRecord::Base
joins(:swf_assets).where(sa[:zone_id].in(zone_ids)).distinct
}
scope :not_occupies, ->(zone_label, locale = I18n.locale) {
# TODO: The perf on this is miserable on its own, the query plan chooses
# a bad index for the join on parents_swf_assets here (but not in the
# `occupies` scope?) and I don't know why! But it makes a better plan when
# combined with `name_includes` so this is probably fine in practice?
# TODO: This is pretty slow! But I imagine it's uncommon so that's probably
# fine in practice? Querying for "has NO records matching X" is hard!
zone_ids = Zone.matching_label(zone_label, locale).map(&:id)
i = Item.arel_table
sa = SwfAsset.arel_table
joins(:swf_assets).where(sa[:zone_id].not_in(zone_ids)).distinct
psa = ParentSwfAssetRelationship.arel_table
subquery = SwfAsset.select('COUNT(*)').joins(:parent_swf_asset_relationships).
where(psa[:parent_type].eq('Item')).where(psa[:parent_id].eq(i[:id])).
where(sa[:zone_id].in(zone_ids))
where("(#{subquery.to_sql}) = 0")
}
scope :restricts, ->(zone_label, locale = I18n.locale) {
zone_ids = Zone.matching_label(zone_label, locale).map(&:id)

View file

@ -29,6 +29,7 @@ class SwfAsset < ActiveRecord::Base
converts_swfs :size => IMAGE_SIZES[:large], :output_sizes => IMAGE_SIZES.values
belongs_to :zone
has_many :parent_swf_asset_relationships
scope :includes_depth, -> { includes(:zone) }