0

I'd like to chain multiple queries together that are on different models is this possible?

Example: I have Restaurants that have many Dishes. I want to search for restaurants that are close to a location and then search for the dishes those restaurants have that match a query. I'm using geocoder and PGSearch for both of the queries, but I don't know how to get them to work together so that I have one query instead of loading all the nearby restaurants into memory and then search by the query.

Thanks!

babaloo
  • 435
  • 5
  • 24

1 Answers1

0

You need to use "joins", I haven't use neither of those gems but I'm going to put a generic active record search and afterwards you can adapt it to yor situation:

Restaurante.where(query_on_restaurante).joins(:dishes).where(dishes:{query_on_dishes})

where:

query_on_restaurantes can be something like : location:"Hawai" :dishes is assumed to be the name of the relationship (restaurante has_many dishes) query_on_dishes can be something like : name:"Cheetos"

NOTICE the brackets on the second where statement.

Myxoh
  • 93
  • 9