Performing nested queries in rails

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

If possible try with :conditions parameter.

T.Veeraa
http://tinyurl.com/25vma7h

Response inline below:

On Fri, Sep 17, 2010 at 9:52 AM, vishy [email protected]
wrote:

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

Where method is available on Model class.

Use

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


Srikanth S.
http://www.srikanthps.com
@srikanthps

[…] 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/
Array.html) and you’ll probably find what you’re looking for.

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.

On Sep 17, 6:22 am, vishy [email protected] 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