Problem using each_with_geodist

27 views
Skip to first unread message

erik

unread,
Feb 17, 2011, 4:38:14 PM2/17/11
to Thinking Sphinx
I upgraded to Rails 2.3.8 and thinking_sphinx gem 1.3.11

I was using Rails 2.2.2 and an old thinking_sphinx plugin.

Most things are working fine, but when i try to use each_with_geodist
I get this error:
NoMethodError: undefined method `each_with_geodist' for #<Array:
0x10c6f9408>

I saw this post about the resolution was to upgrade...
http://groups.google.com/group/thinking-sphinx/tree/browse_frm/month/2009-03/72e3f0b4c837c39d?rnum=111&_done=%2Fgroup%2Fthinking-sphinx%2Fbrowse_frm%2Fmonth%2F2009-03%3F

The code where I'm having the issue
tmp_dealerships = Dealership.search( make_name,
:with => {:makes_id => make_id, :disabled =>
false,"@geodist" => last_miles_in_meters..miles_in_meters},
:order => "offer_count desc,time_to_offer
asc,@geodist asc",
:per_page => number_of_dealerships_to_add *
3, :page => 1,
:geo =>
[ degrees_to_radians(zip_code.latitude),
degrees_to_radians(zip_code.longitude)]).compact

##create hash of the dealership stuff because a ts collection is
not exactly an active record collection
tmp_dealerships.each_with_geodist {|td,dist| ##line throwing the
error
.
.
.
.}

I am using mislav-will_paginate for other parts of my site. Could
this be the issue?

Thanks.
Erik Landerholm

erik

unread,
Feb 17, 2011, 4:54:04 PM2/17/11
to Thinking Sphinx
I figured it out...

tmp_dealerships = Dealership.search( make_name,:with => {:makes_id =>
make_id, :disabled => false,"@geodist" =>
last_miles_in_meters..miles_in_meters},:order => "offer_count
desc,time_to_offer asc,@geodist asc",:per_page =>
number_of_dealerships_to_add * 3, :page => 1,:geo =>
[ degrees_to_radians(zip_code.latitude),
degrees_to_radians(zip_code.longitude)]).compact

puts tmp_dealerships.class # Array

changes to this:

tmp_dealerships = Dealership.search( make_name,:with => {:makes_id =>
make_id, :disabled => false,"@geodist" =>
last_miles_in_meters..miles_in_meters},:order => "offer_count
desc,time_to_offer asc,@geodist asc",:per_page =>
number_of_dealerships_to_add * 3, :page => 1,:geo =>
[ degrees_to_radians(zip_code.latitude),
degrees_to_radians(zip_code.longitude)])

puts tmp_dealerships.class # ThinkingSphinx::Search

I was using compact at some point as I was getting empty
values...probably removing entries, but the indexes were still there.
I used compact to remove the entries. Now it seems to be turning the
thinking_sphinx container into an array. I think before it was a
ThinkingSphinx::Collection of something and it worked just fine.

Erik

On Feb 17, 1:38 pm, erik <e...@carwoo.com> wrote:
> I upgraded to Rails 2.3.8 and thinking_sphinx gem 1.3.11
>
> I was using Rails 2.2.2 and an old thinking_sphinx plugin.
>
> Most things are working fine, but when i try to use each_with_geodist
> I get this error:
> NoMethodError: undefined method `each_with_geodist' for #<Array:
> 0x10c6f9408>
>
> I saw this post about the resolution was to upgrade...http://groups.google.com/group/thinking-sphinx/tree/browse_frm/month/...

erik

unread,
Feb 17, 2011, 11:08:06 PM2/17/11
to Thinking Sphinx
Just in case anyone else ever has this same issue....probably won't,
but who knows! :)

I don't know if this is the best way, but i just added my own
search.rb under initializers directory in my rails project.
In that file I put this:
module ThinkingSphinx
class Search
def compact
sphinx_results = populate
return if sphinx_results.blank?
sphinx_results.each_with_index {|result,index|
sphinx_results.delete_at(index) if result.nil? }
self
end
end
end

It seems to work.

If there is a better way I would appreciate the input.

thanks
erik

Arthur

unread,
Apr 26, 2012, 8:43:02 AM4/26/12
to thinkin...@googlegroups.com
I have the same issue! When trying to do a delete_if on some search results I want to filter out. The ThinkingSphinx collection gets converted into an array and I loose the ability to do a each_with_geodist on it.

I've tried creating config/initializers/search.rb in which I wrote  the following

module ThinkingSphinx
  class Search
    def filter_offline
      sphinx_results = populate
      return if sphinx_results.blank?
      sphinx_results.each_with_index {|result,index|
        sphinx_results.delete_at(index) if !result.user.online? }
      self
    end
  end
end

but I get a undefined method `filter_offline' for #<ThinkingSphinx::Search:0xb223c398>
Reply all
Reply to author
Forward
0 new messages