I have two models, Apartments and Amenities, which are associated through ApartmentAmenities. I am trying to implement a filter where I only show apartments that have all of the amenities specified.
class Amenity < ActiveRecord::Base
has_many :apartments, through: :apartment_amenities
class ApartmentAmenity < ActiveRecord::Base
class Apartment < ActiveRecord::Base
has_many :amenities, through: :apartment_amenities
Apartment.joins(:apartment_amenities).where('apartment_amenities.amenity_id IN (?)', [1,2,3])
Alright, after giving up for a few days then getting back to it, I finally found this question: How to find records, whose has_many through objects include all objects of some list?
Which led me to the answer that works properly:
def self.with_amenities(amenity_ids) where("NOT EXISTS (SELECT * FROM amenities WHERE NOT EXISTS (SELECT * FROM apartment_amenities WHERE apartment_amenities.amenity_id = amenities.id AND apartment_amenities.apartment_id = apartments.id) AND amenities.id IN (?))", amenity_ids) end