# Performing nested queries in rails

**URL:** <https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426>\
**Category:** rubyonrails-talk\
**Created:** [September 17, 2010, 4:22am UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426 "2010-09-17T04:22:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishy](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@vishy](https://discuss.rubyonrails.org/u/vishy)\
**Post date:** [September 17, 2010, 4:22am UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426/1 "2010-09-17T04:22:34Z")

</div>

I am fairly new to rails and trying to learn rails by creating some projects on my own. I am trying to perform a geo search on a restaurant model and I am using geokit for that. My model stores the latitude and longitude for the location.

I am searching for the specified location entered by the user and getting all the restaurants that are near that location. However I would also like to apply another filter on the returned result like returning the restaurant that only serves italian (for example). Here is a snippet of the code that I using.

&nbsp;&nbsp;&nbsp;&nbsp;@location = Geokit::Geocoders::GoogleGeocoder.geocode (params[:location])

&nbsp;&nbsp;&nbsp;&nbsp;//Using geokit find method to return the restaurants within 2 miles of the specified address &nbsp;&nbsp;&nbsp;&nbsp;@restaurants = Restaurant.find(:all, :origin =\> @location, :within =\> 2) //finally getting the restaurants that serve the user specified cuisine &nbsp;&nbsp;&nbsp;&nbsp;@restaurants = @restaurants.where("cuisine like ?", params[:cuisine]) if params[:cuisine]

This results in an " undefined method `where' " error.

I am not able to figure out how can I get this to work. I am using rails3 and ruby 1.9.2.

Any help on this will be greatly appreciated.

Thanks, vishy

---

<div class="post-metadata">

**Author:** ![Srikanth](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@Srikanth](https://discuss.rubyonrails.org/u/Srikanth)\
**Post date:** [September 17, 2010, 5:55am UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426/2 "2010-09-17T05:55:09Z")

</div>

Response inline below:

> I am fairly new to rails and trying to learn rails by creating some projects on my own. I am trying to perform a geo search on a
> 
> restaurant model and I am using geokit for that. My model stores the latitude and longitude for the location.
> 
> I am searching for the specified location entered by the user and getting all the restaurants that are near that location. However I
> 
> would also like to apply another filter on the returned result like returning the restaurant that only serves italian (for example). Here is a snippet of the code that I using.
> 
> @location = Geokit::Geocoders::GoogleGeocoder.geocode
> 
> (params[:location])
> 
> //Using geokit find method to return the restaurants within 2 miles of the specified address @restaurants = Restaurant.find(:all, :origin =\> @location, :within =\> 2) //finally getting the restaurants that serve the user specified
> 
> cuisine @restaurants = @restaurants.where(“cuisine like ?”, params[:cuisine]) if params[:cuisine]

Where method is available on Model class. [http://guides.rubyonrails.org/active\_record\_querying.html#pure-string-conditions](http://guides.rubyonrails.org/active_record_querying.html#pure-string-conditions)

Use

@restaurants = Restaurant.where(“cuisine like ?”,params[:cuisine]) if params[:cuisine]

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [September 17, 2010, 8:42am UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426/3 "2010-09-17T08:42:35Z")

</div>

If possible try with :conditions parameter.

T.Veeraa

> **[Veerasundaravel's Ruby on Rails Weblog](https://veerasundaravel.wordpress.com)**
>
> Ruby on Rails Weblog for Ebooks, Ruby Tips, Rails Tips and more.

---

<div class="post-metadata">

**Author:** ![vishy](https://avatars.discourse-cdn.com/v4/letter/v/b487fb/32.png) [@vishy](https://discuss.rubyonrails.org/u/vishy)\
**Post date:** [September 17, 2010, 3:12pm UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426/4 "2010-09-17T15:12:39Z")

</div>

Restaurant.where(:cuisine =\> params[:cuisine]) will return all the Restaurants that that have the cuisine. However I am trying to filter the returned results from @restaurants = Restaurant.find(:all, :origin =\> @location, :within =\> 2) . Hence I am trying to apply the where condition on @restaurants, using @restaurants = @restaurants.where("cuisine like ?", params[:cuisine]). But that doesn't work.

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [September 18, 2010, 3:38pm UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426/5 "2010-09-18T15:38:36Z")

</div>

> [...] Hence I am trying to apply the where condition on @restaurants, using @restaurants = @restaurants.where("cuisine like ?", params[:cuisine]). But that doesn't work.

Once you've gotten @restaurants you are not querying the DB anymore, hence the 'where' should give you an error. @restaurants is in essence an array and will not have a 'where' method in it unless you add it. Look into the Ruby methods for Array ([http://ruby-doc.org/core/classes/](http://ruby-doc.org/core/classes/) Array.html) and you'll probably find what you're looking for.

---

<div class="post-metadata">

**Author:** ![Robert\_Pankowecki](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@Robert\_Pankowecki](https://discuss.rubyonrails.org/u/Robert_Pankowecki)\
**Post date:** [September 19, 2010, 8:08am UTC](https://discuss.rubyonrails.org/t/performing-nested-queries-in-rails/54426/6 "2010-09-19T08:08:34Z")

</div>

AR.find(:all) returns an array. Ar.where returns an ActiveRecord::Relation proxy which can be enumerate after calling `all` method.

I think that you have few options.

a) reverse the logice because `find` can be called on relation (I think...) :

@restaurants = Restaurant.scoped @restaurants = @restaurants.where("cuisine like ?", params[:cuisine]) if params[:cuisine] @restaurants = @restaurants.find(:all, :origin =\> @location, :within =\> 2)

This assumes that geokit extend ActiveRecord::Relation#find also, not only ActiveRecord::Base#find.

b) put the logic into find conditions = params[:cuisine] ? Restaurant.where("cuisine like ?", params[:cuisine]) : {} @restaurants = Restaurant.find(:all, :origin =\> @location, :within =\> 2, :conditions =\> conditions)

c) use with\_scope condition = params[:cuisine] ? Restaurant.where("cuisine like ?", params[:cuisine]) : Restaurant.scoped Restaurant.with\_scope(:find =\> condition) do &nbsp;&nbsp;@restaurants = Restaurant.find(:all, :origin =\> @location, :within =\> 2) end

with\_scope might be a private method in which case you should call it using 'send'

I hope that at least one option should work fine.

Robert Pankowecki
