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 committed by Matchu
parent b244057808
commit c581b063c4
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 joins(:swf_assets).where(sa[:zone_id].in(zone_ids)).distinct
} }
scope :not_occupies, ->(zone_label, locale = I18n.locale) { scope :not_occupies, ->(zone_label, locale = I18n.locale) {
# TODO: The perf on this is miserable on its own, the query plan chooses # TODO: This is pretty slow! But I imagine it's uncommon so that's probably
# a bad index for the join on parents_swf_assets here (but not in the # fine in practice? Querying for "has NO records matching X" is hard!
# `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?
zone_ids = Zone.matching_label(zone_label, locale).map(&:id) zone_ids = Zone.matching_label(zone_label, locale).map(&:id)
i = Item.arel_table i = Item.arel_table
sa = SwfAsset.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) { scope :restricts, ->(zone_label, locale = I18n.locale) {
zone_ids = Zone.matching_label(zone_label, locale).map(&:id) 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 converts_swfs :size => IMAGE_SIZES[:large], :output_sizes => IMAGE_SIZES.values
belongs_to :zone belongs_to :zone
has_many :parent_swf_asset_relationships
scope :includes_depth, -> { includes(:zone) } scope :includes_depth, -> { includes(:zone) }