2012-10-26 2 views
4

이 많은 Items 인 레일 앱을 개발 중입니다. 루비 지오 코더 젬에 의존하는 Place이라는 다른 모델 덕분에 사용자가 지오 코딩되었습니다. 사용자 모델에서 지오 코더 : 거리 단위로 정렬 가능

class Place < ActiveRecord::Base 
    attr_accessible :street_number, :street, :city, :postal_code, :country, :latitude, :longitude 

    belongs_to :placable, :polymorphic => true 
    geocoded_by :address 
    after_validation :geocode 

    def address 
     [street_number, street, postal_code, city, country].compact.join(', ') 
    end 
end 

, 나는 사용자 수준에서 직접 액세스 할 수 있도록 Place 모델의 방법을 위임.

class User < ActiveRecord::Base 
    has_many :items, :dependent => :destroy 
    has_one :place, :as => :placable, :dependent => :destroy 
    accepts_nested_attributes_for :place 
    attr_accessible :place_attributes 

    geocoded_by :address 
    delegate :address, :to => :place, :allow_nil => true 
    delegate :latitude, :to => :place, :allow_nil => true 
    delegate :longitude, :to => :place, :allow_nil => true 
    #... 
end 

같은 트릭은 항목 수준에서 사용됩니다

class Item < ActiveRecord::Base 
    belongs_to :user 

    # place 
    geocoded_by :address 
    delegate :place, :to => :user, :allow_nil => true 
    delegate :address, :to => :user, :allow_nil => true 
    delegate :latitude, :to => :user, :allow_nil => true 
    delegate :longitude, :to => :user, :allow_nil => true 
    # ... 
end 

지금 나는 그들이 현재 사용자의 거리상으로 분류되도록 항목에 대한 인덱스를 설정하고 싶습니다. 이 설치에서 작업을 수행 할 디자인을 찾지 못했습니다.

은 내가 latitudelongitudeitems 테이블의 열이없는 것을 불평 실패 User 모델 지오 코더에서 near 방법의 위임과 Item 모델하지만 SQL 쿼리로 다음을 수행하려고 노력했다. 실제로는 정확하지만 대신 places 테이블의 정보에 액세스하고 싶습니다.

# this added to the user model: 
    delegate :near :to => :place 
    # this added to the item model: 
    delegate :near :to => :user 

    class ItemsController < ApplicationController 

    def index 
     @items=Item.near(current_user.place, params[:distance]).paginate(:per_page => 20, :page => params[:page_name]) 
    end 
    end 

사용자와 항목 모두 지오 코딩 (위도, 경도, 주소 등)에 대한 속성을 갖는 솔루션을 피하고 싶습니다. 나는 그것이 나에게 너무 분명 또는 내가 틀렸다, 그러나 저 시도 허용하는 경우 몰라요

+0

어떤 데이터베이스를 사용하고 있습니까? Postgres에는 문제를 해결하는 데 도움이되는 지리 정보 모듈이 있습니다. – dpassage

+0

@dpassage - 실제로 Postgres에 있습니다. 언급 한 모듈에 대한 자세한 정보를 제공해 주시겠습니까? 쓰레드 – microcosme

+0

몇 가지. hardcore에 대한 PostGIS (http://postgis.refractions.net) 또는 덜 하드 코어에 대한 표준 Postgres 확장 earthdistance (http://www.postgresql.org/docs/9.1/static/earthdistance.html)가 있습니다. – dpassage

답변

0

:

난 그냥 열을 만드는 것 (예를 들어, 위치 - 아이템 모델에서이)의 현재 위치를 저장합니다 항목을 선택한 다음 현재 사용자 위치와 비교하십시오.

훑어 본 것이 아닙니까? 내가 도왔 으면 좋겠어!