diff --git a/app/models/photo_album.rb b/app/models/photo_album.rb index 549edf48..b55797de 100644 --- a/app/models/photo_album.rb +++ b/app/models/photo_album.rb @@ -12,7 +12,17 @@ class PhotoAlbum < ApplicationRecord scope :publicly_visible, -> { where(publicly_visible: true) } scope :without_photo_tags, lambda { - where.not(id: Photo.joins(:tags).select(:photo_album_id).distinct) + qualifying_album_ids_subquery = unscoped + .joins(:photos) + .left_joins(photos: :tags) + .group('photo_albums.id') + .having(<<~SQL.squish) + ( + COALESCE(COUNT(DISTINCT photo_tags.photo_id), 0) * 1.0 / COUNT(DISTINCT photos.id) + ) < 0.85 + SQL + .select('photo_albums.id') + where(id: qualifying_album_ids_subquery) } def owners