Reputation: 73
I'm building a rails application for searching for hiking trails. I have two models, a location model and a trail model. A location can have many trails, and a trail belongs to one location.
How would I implement a search functions so I could search for all location with a trail at least X miles long or with Y feet of elevation change? I'm unsure of how to limit a query on the location model by the attributes of its nested resource/submodel.
Upvotes: 0
Views: 46
Reputation: 669
So you want all locations that hace at least a trail that matches the specific conditions, right? this is how you can achieve that:
Location.where(id: Trail.where('miles > ? AND elevation_change > ?', 10, 100).pluck(:location_id))
just replace miles and elevation_change for your actual attribute names and conditions
Upvotes: 1