「距离」将 Geokit 与acts_as_mappable :through 一起使用时,字段会被删除

发布于 2024-11-07 07:25:06 字数 389 浏览 0 评论 0原文

我们知道,当在Rails 中使用带有acts_as_mappable :through 模型类的Geokit gem 时,距离字段会被丢弃。我想知道是否有办法解决这个问题以恢复距离场。我尝试遵循这里的猴子修补示例: http://www.sobyteme.com/news/2010/05/13/computers/2010/06/25/geokit-acts_as_mappable-through-with-distance-attribute/ 但这对我不起作用。

We know that the distance field gets dropped when using the Geokit gem in Rails with acts_as_mappable :through model class. I wonder if there's a way to work around this to get the distance field back. I tried to follow the monkey-patching example over here:
http://www.sobyteme.com/news/2010/05/13/computers/2010/06/25/geokit-acts_as_mappable-through-with-distance-attribute/
but it didn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-11-14 07:25:06

好吧,史蒂夫在他的网站上的建议是准确的,我在完成查找后错过了sort_by_distance_from的电话。所以这个答案归功于他。

我使用的是 Rails v3.0.7。这是我的代码:

class Office < ActiveRecord::Base
    has_many :users
    acts_as_mappable :default_units => :miles, 
                     :default_formula => :sphere, 
                     :lat_column_name => :latitude,
                     :lng_column_name => :longitude
end

class User < ActiveRecord::Base
    belongs_to  :office
    acts_as_mappable :through => :office
end

users_controller.rb:

# Monkey patching to include the 'distance' attribute
module Geokit
  module Mappable
    def to_lat_lng
      return self if instance_of?(Geokit::LatLng) || instance_of?(Geokit::GeoLoc)
      return LatLng.new(self.office.send(self.office.class.lat_column_name),
        self.office.send(self.office.class.lng_column_name)) if self.class.respond_to?(:acts_as_mappable)
      nil
    end
  end 
end

class UsersController < ApplicationController

  def location
    @lat = params[:lat].to_f
    @long = params[:long].to_f
    @origin = [@lat, @long]
    @users = User.find(:all, 
         :origin => @origin,
         :conditions => "distance < 3")

    # We have to add this to get the 'distance' field
    @users.sort_by_distance_from(@origin)

    respond_to do |format|
      format.html
      format.xml  { render :xml => @users.to_xml(:methods => :distance)}
      format.json  { render :json => @users.to_json(:methods => :distance)}
    end
  end 
...
end

Well, Steve's suggestion over on his site was accurate, I was missing calling sort_by_distance_from after doing the find. So credit goes to him for this answer.

I'm on Rails v3.0.7. Here's my code:

class Office < ActiveRecord::Base
    has_many :users
    acts_as_mappable :default_units => :miles, 
                     :default_formula => :sphere, 
                     :lat_column_name => :latitude,
                     :lng_column_name => :longitude
end

class User < ActiveRecord::Base
    belongs_to  :office
    acts_as_mappable :through => :office
end

users_controller.rb:

# Monkey patching to include the 'distance' attribute
module Geokit
  module Mappable
    def to_lat_lng
      return self if instance_of?(Geokit::LatLng) || instance_of?(Geokit::GeoLoc)
      return LatLng.new(self.office.send(self.office.class.lat_column_name),
        self.office.send(self.office.class.lng_column_name)) if self.class.respond_to?(:acts_as_mappable)
      nil
    end
  end 
end

class UsersController < ApplicationController

  def location
    @lat = params[:lat].to_f
    @long = params[:long].to_f
    @origin = [@lat, @long]
    @users = User.find(:all, 
         :origin => @origin,
         :conditions => "distance < 3")

    # We have to add this to get the 'distance' field
    @users.sort_by_distance_from(@origin)

    respond_to do |format|
      format.html
      format.xml  { render :xml => @users.to_xml(:methods => :distance)}
      format.json  { render :json => @users.to_json(:methods => :distance)}
    end
  end 
...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文