activerecord-hackery / squeel

Active Record, improved. Live again :)
http://erniemiller.org/2013/11/17/anyone-interested-in-activerecord-hackery/
MIT License
2.4k stars 213 forks source link

Filter using a model array? #391

Open simkessy opened 9 years ago

simkessy commented 9 years ago

I'm trying to filter results based on an array of models

I have 4 models: Venues, Facility, Availability and Activities.

This is the squeel I put together to return Availabilities:

@search = Availability.joins{facility.venue.activities}.where{(activities.id == activity) & (booking_id == nil) & (start_time <= datetime )}    

I'm trying to figure out how to filter the results based on an array of Venues which are in proximity to the location entered.

I can get a list of Venues using geocoder gem: venues = Venue.near("Some location") , how would I add it above to get Availabilities only from the returned venues?

simkessy commented 9 years ago

I solved it doing this:

@search = Availability.joins{facility.venue.activities}
                      .where{
                        (activities.id == s_activity) &
                        (booking_id == nil) &
                        (start_time >= s_start_time ) &
                        (end_time <= s_end_time ) &
                        (facility.venue_id.in near_by_venues)
                      }
                      .order{start_time.desc}

If anyone has any suggestions on how I could improve this, that would be great. Thanks.