activerecord - Ruby on Rails query not working properly -
i have several listings, , define number of filters. in particular, listing has_many :spaces, through: :designations , has_many :amenities, through: :offerings.
i use filters restrict listings shown.
the 2 main ones are:
# filter space type if params[:search][:space_ids].present? && params[:search][:space_ids].reject(&:blank?).size > 0 @listings = @listings.joins(:spaces).where('space_id in (?)', params[:search][:space_ids].reject(&:blank?)).uniq end # filter amenities if params[:search][:amenity_ids].present? && params[:search][:amenity_ids].reject(&:blank?).size > 0 @listings = @listings.joins(:amenities).where(amenities: { id: params[:search][:amenity_ids].reject(&:blank?) }).group('listings.id').having('count(*) = ?', params[:search][:amenity_ids].reject(&:blank?).size) end the first filter says: of listings match of selected space types.
the second filter says: of listings have of selected amenities.
these filters work part, not always. in particular, suppose listing has space types 1 , 2 , amenity 2. if filter space types 1 , 2 (so space_ids: ['1', '2', '']) , amenity 2 (so amenity_ids: ['2', '']), get: #<activerecord::relation []>.
but should listing a. wrong query?
basically, trying implement this: complex rails query using activerecord on many many relationship
or this: http://blog.hasmanythrough.com/2006/6/12/when-associations-arent-enough-part-2
changing having('count(*) = ?', params[:search][:amenity_ids].reject(&:blank?).size) having('count(*) >= ?', params[:search][:amenity_ids].reject(&:blank?).size) (i.e., = >=) fixes issue.
but there issue: ruby on rails query yielding unexpected results.
Comments
Post a Comment