2017-04-24 67 views
0

我试图找到令的距离内的所有车手pickup_address',作为GeoKit引导https://github.com/geokit/geokit-rails#using-throughGeokit位置未定义的方法`LAT”与:通过

司机们通过UserLocations和订单已经地点在解释通过PickupAddresses的位置。

我收到以下错误:

NoMethodError in OrdersController#show 
undefined method `lat' for "Order" 

Order.rb

class Order < ApplicationRecord 

has_paper_trail 

# an order MUST contain the following 
validates_presence_of :description 
validates_presence_of :weight 
validates_presence_of :length 
validates_presence_of :width 
validates_presence_of :height 
validates_presence_of :pickup_address 
validates_presence_of :dropoff_address 
validates_presence_of :pickup_contact 
validates_presence_of :dropoff_contact 

# each order should have one address for pickup and one address for dropoff 
has_one :pickup_address, :as => :locatable # also works for belongs_to associations 
acts_as_mappable :through => :pickup_address 
has_one :dropoff_address, inverse_of: :order 

# each order needs two contacts 
has_one :pickup_contact, inverse_of: :order 
has_one :dropoff_contact, inverse_of: :order 
end 

PickupAddress.rb

class PickupAddress < Address 
    belongs_to :locatable, :polymorphic => true 
    acts_as_mappable 
end 

我还添加了acts_as_mappable到Address.rb,看看该代码将工作,但没有任何改变。

class Address < ApplicationRecord 
    belongs_to :locatable, :polymorphic => true 
    acts_as_mappable 

    # Attributes to have a valid address for Driver/Geocoder 
    validates_presence_of :street1 
    validates_presence_of :city 
    validates_presence_of :state 
    validates_presence_of :country 
    validates_presence_of :postal 
    validates_presence_of :type 

    # Geocoder conversion for easier calculations 
    geocoded_by :full_address 
    after_validation :geocode 

    # address used by Geocoder and condensed display 
    def full_address 
    [street1, city, state, country].compact.join(', ') 
    end 
end 

这是我得到一个错误就行了:

@drivers = Driver.within(5, :origin => @order) 

而且,以供参考,这是我的驱动器映射:

Driver.rb

class Driver < User 
    has_many :orders 
end 

User.rb

class User < ApplicationRecord 
    # each user has a location, drivers first, then can expand later 
    has_one :user_location, :as => :locatable 
    acts_as_mappable :through => :user_location 
end 

UserLocation.rb

class UserLocation < ApplicationRecord 
    belongs_to :locatable, :polymorphic => true 
    acts_as_mappable :default_units => :miles 

    #reverse_geocoded_by :latitude, :longitude 
end 

最后,模式:

create_table "user_locations", force: :cascade do |t| 
t.integer "user_id" 
t.float "lat" 
t.float "lng" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
t.index ["user_id"], name: "index_user_locations_on_user_id", using: :btree 
end 

create_table "addresses", force: :cascade do |t| 
t.string "type" 
t.string "street1" 
t.string "street2" 
t.string "city" 
t.string "state" 
t.string "postal" 
t.string "country" 
t.integer "order_id" 
t.float "lat" 
t.float "lng" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
t.index ["order_id"], name: "index_addresses_on_order_id", using: :btree 
end 

OrdersController.rb

class OrdersController < ApplicationController 
    layout 'dashboard' 

    before_action :authenticate_user! 

    before_action :set_order, only: [:show, :edit, :update, :destroy] 

    before_action :load_resource 

    # insert generic boring stuff 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_order 
     @order = Order.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def order_params 
     params.require(:order).permit(:invoice, :description, :weight, :length, :width, :height, :account_id, :driver_id, :status, 
     pickup_address_attributes: [:street1, :street2, :city, :state, :postal, :country], 
     dropoff_address_attributes: [:street1, :street2, :city, :state, :postal, :country], 
     pickup_contact_attributes: [:first_name, :last_name, :phone], 
     dropoff_contact_attributes: [:first_name, :last_name, :phone]) 
    end 

    def load_resource 
     case params[:action].to_sym 
     when :show 
     if current_user.is_admin? 
      @drivers = Driver.within(5, :origin => @order) 
     end 
     end 
    end 
end 

新的错误改变了我的纬度和经度列与后 '纬度' 和' lng'建议改变:

NEW error: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "user_locations" 
LINE 1: ...M "users" WHERE "users"."type" IN ('Driver') AND (user_locat... 
                 ^
: SELECT "users".* FROM "users" WHERE "users"."type" IN ('Driver') AND (user_locations.lat IS NOT NULL AND user_locations.lng IS NOT NULL) AND (user_locations.lat>29.61103991439227 AND user_locations.lat<38.28523108560773 AND user_locations.lng>-88.97349091777367 AND user_locations.lng<-78.5259326822263) AND (((ACOS(least(1,COS(0.5925067393881714)*COS(-1.4617082185063466)*COS(RADIANS(user_locations.lat))*COS(RADIANS(user_locations.lng))+ COS(0.5925067393881714)*SIN(-1.4617082185063466)*COS(RADIANS(user_locations.lat))*SIN(RADIANS(user_locations.lng))+ SIN(0.5925067393881714)*SIN(RADIANS(user_locations.lat))))*3963.1899999999996) <= 300)) 
+0

我正要离开指南,位于这里:https://github.com/geokit/geokit-rails –

+0

您需要提供订单控制器。 –

+0

只为你添加控制器! –

回答

0

让我们清楚怎么在这里,你正在尝试运行工作

方法

@drivers = Driver.within(5日:起源=> @order)

起源的属性,您要发送到该方法需要是具有lat和lng属性的模型的实例,并且看起来像order没有它们,在这个示例中,您有两个具有这些属性addressesuser_locations的模型只是为具有以下属性的实例更改@order你的经纬度和你想要的驱动程序

+0

新的错误:未定义的方法'纬度”为#<订单:0x007f8c29af2e88> –

+0

通过添加:pickup_address到该行太,对不起 – xploshioOn

+0

更改良好的措施在列名尝试上述添加剂后,并获得同样的错误。 –