# Performing nested queries in rails

**URL:** <https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112>\
**Category:** Rails\
**Created:** [September 17, 2010, 6:28am UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112 "2010-09-17T06:28:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [September 17, 2010, 6:28am UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112/1 "2010-09-17T06:28:01Z")

</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.

```
@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]

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:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [September 17, 2010, 10:42am UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112/2 "2010-09-17T10:42:35Z")

</div>

If possible try with :conditions parameter.

T.Veeraa  
[http://tinyurl.com/25vma7h](http://tinyurl.com/25vma7h)

---

<div class="post-metadata">

**Author:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [September 17, 2010, 8:08am UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112/3 "2010-09-17T08:08:07Z")

</div>

Response inline below:

On Fri, Sep 17, 2010 at 9:52 AM, vishy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> params[:cuisine]) if params[:cuisine]

Where method is available on Model class.

> **[Active Record Query Interface — Ruby on Rails Guides](https://guides.rubyonrails.org/active_record_querying.html#pure-string-conditions)**
>
> Active Record Query InterfaceThis guide covers different ways to retrieve data from the database using Active Record.After reading this guide, you will know: How to find records using a variety of methods and conditions. How to specify the order,...

Use

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

> 

–  
Srikanth S.  
[http://www.srikanthps.com](http://www.srikanthps.com)  
@srikanthps

---

<div class="post-metadata">

**Author:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [September 18, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112/4 "2010-09-18T17:39:55Z")

</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:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [September 17, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112/5 "2010-09-17T17:13:46Z")

</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:** ![vishy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b487fb/32.png) [@vishy](https://www.ruby-forum.com/u/vishy)\
**Post date:** [September 19, 2010, 10:09am UTC](https://www.ruby-forum.com/t/performing-nested-queries-in-rails/195112/6 "2010-09-19T10:09:34Z")

</div>

On Sep 17, 6:22 am, vishy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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  
@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
