当对象有2组坐标时使用rails geocoder

Jus*_*tin 5 ruby-on-rails-3 rails-geocoder

Rails 3.1,Ruby 1.9.3

所以我有两个模型,都有两组坐标.

第一种模式,Load具有from_lat/lng,以及to_lat/lng.
第二个型号,卡车,current_lat/lng以及destination_lat/lng.

我要做的是找到所有结果,current_lat/lng或者destination_lat/lng来自Load模型的to_lat/lng附近的Truck模型.

我的车道模型是这样的:

class Load < ActiveRecord::Base  

  has_many :trucks

  geocoded_by :custom_geocode  

  after_validation :custom_geocode

  def custom_geocode  
    var = Geocoder.coordinates([from_city,from_state].compact.join(','))  
    var2 = Geocoder.coordinates([to_city,to_state].compact.join(','))  
    self.from_lat = var.first  
    self.from_lng = var.last  
    self.to_lat = var2.first  
    self.to_lng = var2.last  
  end
end
Run Code Online (Sandbox Code Playgroud)

在我看来,我有:

<% for truck in @trucks %>  
  <li><%= truck.destination_lat %></li>  
<% end %>
Run Code Online (Sandbox Code Playgroud)

目前我在load_controller中使用它:

def show
  @load = Load.find(params[:id])
  @trucks = Truck.near([@load.from_lat, @load.from_lng], 100, :order => "distance")

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @load }
  end
end
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:
PG::Error: ERROR: column trucks.latitude does not exist

我知道这是因为该列的名称是不是默认:latitude:longitude.在此特定搜索期间,如何在Truck模型中指定要使用的列集(destination_lat/lng或current_lat/lng)?