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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Response inline below: On Fri, Sep 17, 2010 at 9:52 AM, vishy <shubhambansal15-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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] >Where method is available on Model class. http://guides.rubyonrails.org/active_record_querying.html#pure-string-conditions Use @restaurants = Restaurant.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 > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Srikanth Shreenivas http://www.srikanthps.com @srikanthps -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If possible try with :conditions parameter. T.Veeraa http://tinyurl.com/25vma7h -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, 3:42 am, Veera Sundaravel <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> If possible try with :conditions parameter. > > T.Veeraahttp://tinyurl.com/25vma7h > > -- > Posted viahttp://www.ruby-forum.com/.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> [...] 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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 17, 6:22 am, vishy <shubhambansa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.