However, ActiveRecord drops the calculated distance column when you
use include. So, if you need to use the distance column, youll have
to re-calculate it post-query in Ruby:
stores.sort_by_distance_from(home)
In this case, you may want to just use the bounding box condition
alone in your SQL (theres no use calculating the distance twice):
bounds=Bounds.from_point_and_radius(home,5)
stores=Store.find :all, :include=>[:reviews,:cities] :bounds=>bounds
stores.sort_by_distance_from(home)
This presents a problem because when you use @models = Model.paginate,
applying the sort to the results doesn't seem to do anything.
Does anyone know if there is a workaround to this?
If you want to paginate by distance, you'll have to calculate distance on DB level; doing it in Ruby won't work. You could only do pagination in Ruby if you fetched *all* the records, sorted them and then calling #paginate on an array.
If you could update to Rails 2.1, I think your problem would be solved. ActiveRecord 2.1 uses a preloading strategy for includes that I think would take place here: 3 separate SQL queries would happen, one to load Stores and other two to load Reviews and Cities.
Then maybe ActiveRecord won't drop the calculated column.
> However, ActiveRecord drops the calculated distance column when you > use include. So, if you need to use the distance column, you'll have > to re-calculate it post-query in Ruby: > stores.sort_by_distance_from(home)
> In this case, you may want to just use the bounding box condition > alone in your SQL (there's no use calculating the distance twice): > bounds=Bounds.from_point_and_radius(home,5) > stores=Store.find :all, :include=>[:reviews,:cities] :bounds=>bounds > stores.sort_by_distance_from(home)
> This presents a problem because when you use @models = Model.paginate, > applying the sort to the results doesn't seem to do anything.
> Does anyone know if there is a workaround to this?
Yeah, I ended up having to do exactly as you stated:
@contact = Barn.find(:all,
:conditions => conditions[:conditions],
:include => conditions[:associations],
:bounds => geo_params[:bounds])
@contact.sort_by_distance_from(geo_params[:origin])
@contacts = @contact.paginate(:page => params[:page], :per_page
=> 8)
This fetches all the records (~3000) and then paginates the array.
I'm using Rails 2.0.2 right now. Is 2.1 still considered edge? I
suppose I could give it a try, but this works OK enough for now.
On May 15, 8:04 pm, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:
> If you want to paginate by distance, you'll have to calculate distance on DB
> level; doing it in Ruby won't work. You could only do pagination in Ruby if
> you fetched *all* the records, sorted them and then calling #paginate on an
> array.
> If you could update to Rails 2.1, I think your problem would be solved.
> ActiveRecord 2.1 uses a preloading strategy for includes that I think would
> take place here: 3 separate SQL queries would happen, one to load Stores and
> other two to load Reviews and Cities.
> Then maybe ActiveRecord won't drop the calculated column.
> On Thu, May 15, 2008 at 10:48 PM, thoraxe <erikmjac...@gmail.com> wrote:
> > An excerpt from the geokit RDoc:
> > You can use includes along with your distance finders:
> > However, ActiveRecord drops the calculated distance column when you
> > use include. So, if you need to use the distance column, you'll have
> > to re-calculate it post-query in Ruby:
> > stores.sort_by_distance_from(home)
> > In this case, you may want to just use the bounding box condition
> > alone in your SQL (there's no use calculating the distance twice):
> > bounds=Bounds.from_point_and_radius(home,5)
> > stores=Store.find :all, :include=>[:reviews,:cities] :bounds=>bounds
> > stores.sort_by_distance_from(home)
> > This presents a problem because when you use @models = Model.paginate,
> > applying the sort to the results doesn't seem to do anything.
> > Does anyone know if there is a workaround to this?
It has version number "2.0.991" because it isn't 2.1 final. I had no problems upgrading, it's backwards-compat. It might break 3rd party code like plugins that hack deep into ActiveRecord and ActionView---hope GeoKit still works...
I suppose I could give it a try, but this works OK enough for now.
If fetching *3000* records *with* includes *into memory* for *each page* is "OK" for you, then I don't think there is anything bad in the world by your standards :)
Seriously - you don't want to paginate in Ruby. Try Rails 2.1; if that breaks GeoKit then return here and we'll discuss some more.
The gem update didn't seem to have any abnormal effects. It appears
though that Comcast is at it again, because Google is slow as the
hills, making it hard to test all my Google-based geo-functionality,
but it seems to be working.
So what are you suggesting that I do with the search now?
> It has version number "2.0.991" because it isn't 2.1 final. I had no
> problems upgrading, it's backwards-compat. It might break 3rd party code
> like plugins that hack deep into ActiveRecord and ActionView---hope GeoKit
> still works...
> I suppose I could give it a try, but this works OK enough for now.
> If fetching *3000* records *with* includes *into memory* for *each page* is
> "OK" for you, then I don't think there is anything bad in the world by your
> standards :)
> Seriously - you don't want to paginate in Ruby. Try Rails 2.1; if that
> breaks GeoKit then return here and we'll discuss some more.
First verify that your app is using the latest gem version (in environment.rb).
On Fri, May 16, 2008 at 3:06 AM, thoraxe <erikmjac...@gmail.com> wrote:
> So what are you suggesting that I do with the search now?
With ActiveRecord 2.1 you might find that your initial query on this thread works. I'm just guessing here, but it's worth trying out. Report back with results.
Looks like I need to update the other items as well...
/usr/lib/ruby/site_ruby/1.8/rubygems.rb:379:in
`report_activate_error': RubyGem version error: activesupport(2.0.2
not = 2.0.991) (Gem::LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:311:in `activate'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:337:in `activate'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:336:in `each'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:336:in `activate'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:65:in
`active_gem_with_options'
from /usr/lib/ruby/site_ruby/1.8/rubygems.rb:50:in `gem'
from ./script/../config/boot.rb:29
from script/server:2:in `require'
from script/server:2
OK well, everythign is updated, but it unfortunately appears to be
ActiveScaffold that is the issue, not GeoKit :(
thoraxe@vmdora ~/railsapps/equine $ script/server -d
=> Booting WEBrick...
/usr/lib/ruby/gems/1.8/gems/activesupport-2.0.991/lib/active_support/
core_ext/module/aliasing.rb:31:in `alias_method': undefined method
`find_full_template_path' for class `ActionView::Base' (NameError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.991/lib/
active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
from /home/thoraxe/railsapps/equine/vendor/plugins/
active_scaffold/lib/extensions/generic_view_paths.rb:26
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.991/lib/
active_support/dependencies.rb:508:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.991/lib/
active_support/dependencies.rb:353:in `new_constants_in'
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.991/lib/
active_support/dependencies.rb:508:in `require'
from /home/thoraxe/railsapps/equine/vendor/plugins/
active_scaffold/environment.rb:63
... 31 levels...
from /usr/lib/ruby/gems/1.8/gems/rails-2.0.991/lib/commands/
server.rb:39
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
from script/server:3
Either I did something wrong or it doesn't work. But, until AS can
work with the latest rails, I guess I'm stuck doing something I
SERIOUSLY don't want to do :)
> Either I did something wrong or it doesn't work. But, until AS can
> work with the latest rails, I guess I'm stuck doing something I
> SERIOUSLY don't want to do :)
I've recently updated both ActiveScaffold as well as Rails. I'm also
likely running the latest will_paginate gem. Everything is "working,"
except I have a horrendous memory leak issue with my ActiveScaffold
admin area.
If I can get that sorted soon, I'll be back in touch regarding this
particular issue.
Thanks for your help thus far. I'm sure we can get it hammered out.
On Jun 4, 10:28 pm, Bill <wrobb...@gmail.com> wrote: