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 => …Run Code Online (Sandbox Code Playgroud)