2014-12-30 1 views
0

좋아, 내가 가지고있는 것은 Truckers가 트럭을 게시 할 수있는 Trucking Load 보드입니다. 나는 트럭을 게시하고있다. 하지만 검색 기능을 설정하는 데 문제가 있고 다른 테이블을 연결해야하는 방식입니다.Geokit-Rails를 사용하여 원본 및 목적지 위치로 게시물 찾기

Rails 3 
postgres 

gem 'geokit-rails' 

나는 그것을이 방법은 지금은 같은 위치 테이블 설정을 가지고 있습니다 :

class Location < ActiveRecord::Base 

    attr_accessible :cs, :lat, :lon, :city, :state 

    acts_as_mappable :default_units => :miles, 
        :default_formula => :sphere, 
        :distance_field_name => :distance, 
        :lat_column_name => :lat, 
        :lng_column_name => :lon 

누군가가 그것을 가지고 트럭과 기원과 대상을 게시 할 때. 그래서이 개 위치가이처럼 설정 한 : 그것은 관련된 위치 기록을 반환합니다

Truck.find(1).origin || Truck.find(1).dest 

: 나는 그것을 내가로부터 위치 정보를 얻을 수 있습니다 설정 한 방법으로

class Truck < ActiveRecord::Base 
    attr_accessible :available, :company_id, :dest_id, :origin_id, :equipment, :origin, :dest 
    belongs_to :origin, class_name: "Location", foreign_key: :origin_id 
    belongs_to :dest, class_name: "Location", foreign_key: :dest_id 
    belongs_to :company 

그것과 함께

이제 내 문제는 내가 원산지에서 "주어진"금액 내에서 트럭을 찾기 위해 검색 기능을 쓸 수 있기를 원한다는 것입니다. 목적지 || 원산지 & 대상

난 내가 Location.within(25, :origin => "Springfield, Mo")을 할 수 있으며 모든 위치를 검색하고 스프링 모의 이십오마일

그러나이 개 위치가 어디에 어떻게 내가 트럭에이을 사용 내에있는 사람을 반환합니다 알고 (원점 & dest)이며 위치 ID와 연관되어 있습니다.

나는 현재 다른 검색에 Params 이미 코딩 그리고 난 그것으로이 통합 수있는 방법 만 확실하지 작업 한 :

def search(search) 
    where = [] 
    where << PrepareSearch.states('dest', search.dest_states) unless search.dest_states.blank? 
    where << PrepareSearch.states('origin', search.origin_states) unless search.origin_states.blank? 
    where << PrepareSearch.location('origin', search.origin_id, search.radius) unless search.origin.blank? 
    where << PrepareSearch.location('dest', search.dest_id, search.radius) unless search.dest.blank? 
    where << PrepareSearch.equipment(search.equipment) unless search.equipment.blank? 
    where << PrepareSearch.date('available', search.available, '<') unless search.available.blank? 
    where = where.join(' AND ') 
    Truck.where(where) 
    end 


module PrepareSearch 
    def PrepareSearch.location(type, location, radius) 
     loc = Location.find(location) 

    *type will be origin/destination Location active record 
    *location will be Location id 
    *radius will be a given mileage 

**This is where i need to figure out what to put here** 

    end 
end 

그것은 더 좋을 것이다 단지 방정식을 통합 :

def sphere_distance_sql(origin, units) 
    lat = deg2rad(origin.lat) 
    lng = deg2rad(origin.lng) 
    multiplier = 3963.1899999999996 # for miles 

    sphere_distance_sql(lat, lng, multiplier) 
    end 
    def sphere_distance_sql(lat, lng, multiplier) 
    %| 
     (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+ 
     COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+ 
     SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier}) 
    | 
    end 

답변

1

좋아 그럼 내 문제에 대한 해결책을 찾았습니다 ... 알고 싶다면 더 좋은 정보가 있으면 알려주세요.

where << PrepareSearch.location_ids("origin", search.origin, search.radius) unless search.origin_id.blank? 
where << PrepareSearch.location_ids("dest", search.dest, search.radius) unless search.dest_id.blank? 


def PrepareSearch.location_ids(type, location, radius) 
    if location.nil? 
    return nil 
    else 
    loc = Location.find(location) 
    location(type, loc, radius) 
    end 

end 

def PrepareSearch.location(type, location, radius) 
    locs = Location.within(radius, :origin => location.cs).pluck(:id) 
    st = "" 
    locs.each {|s| st += "'#{s}'," } 
    st = st[0..-2] 
    "#{type}_id IN (#{st})" 
end 
관련 문제