On Sep 17, 6:22 am, vishy <[email protected]> wrote:
> 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.
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
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.