2011-05-15 2 views
1

레일스에서 ​​Geokit 젬을 acts_as_mappable : through 모델 클래스와 함께 사용하면 거리 필드가 삭제된다는 것을 알고 있습니다. 거리 필드를 되찾기 위해이 문제를 해결할 방법이 있는지 궁금합니다. 나는 여기에 원숭이 - 패칭의 예를 따라 갔다 : http://www.sobyteme.com/news/2010/05/13/computers/2010/06/25/geokit-acts_as_mappable-through-with-distance-attribute/ 그러나 그것은 나를 위해 작동하지 않았다.acts_as_mappable과 함께 Geokit을 사용할 때 'distance'필드가 누락 됨 :

+0

난 당신이 gmaps4rails 보석을 살펴이 좋습니다. – apneadiving

+0

불행히도 gmaps4rails gem은 제가 게시 한 질문과 아무 관련이 없습니다. 그들은 정말로 무관합니다. –

+0

미안,'act_as_mappable'이지도를 만들었다 고 생각했습니다. – apneadiving

답변

1

글쎄, 그의 사이트에 스티브의 제안이 정확했다, 나는 찾은 후 sort_by_distance_from을 호출 한 후 실종되었다. 그래서이 답은 신용이 ​​그에게 간다.

레일즈 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 
관련 문제