NoMethodError 使用 activerecord_postgis_adapter

NoMethodError using activerecord_postgis_adapter

我尝试按照 README 安装 activerecord-postgis-adapter,但无法创建模型实例,因为我不断收到以下提到的错误:

NoMethodError: undefined method `point' for nil:NilClass

这是我的不同文件的样子:

Location.rb(模型,更新代码)

# == Schema Information
#
# Table name: locations
#
#  id             :integer          not null, primary key
#  locatable_id   :integer
#  locatable_type :string
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  coordinates    :geography({:srid point, 4326
#

class Location < ActiveRecord::Base

    #self.rgeo_factory_generator = RGeo::Geos.factory_generator
    #set_rgeo_factory_for_column(:coordinates, 
    #   RGeo::Geographic.spherical_factory(:srid => 4326) )

    locFactory = RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(:geo_type => 'point')

    belongs_to  :locatable,
        polymorphic: true

    validates   :locatable, presence: true

    attr_accessor   :longitude, :latitude

end

宝石文件

gem 'rgeo'
gem 'rgeo-activerecord'
gem 'activerecord-postgis-adapter'

config/application.rb

require 'rails/all'
#require 'active_record/connection_adapters/postgis_adapter/railtie'
#require "#{Rails.root}/lib/rgeo"

config/database.yml

development:
<<: *default
database: geoproject-api_development
adapter: postgis
schema_search_path: "public,postgis"

在我尝试检索记录时添加记录后,我得到以下信息:

irb(main):003:0> lala = Location.first
Location Load (1.6ms) SELECT "locations".* FROM "locations" ORDER BY "locations"."id" ASC LIMIT 1
(Object doesn't support #inspect) 
irb(main):004:0> lala.class.name => "Location" 
irb(main):005:0> puts lala
#<Location:0x007fdfd4bb2860>
=> nil
irb(main):006:0> puts lala.inspect
NoMethodError: undefined method point' for nil:NilClass

想知道为什么会这样或者我该如何解决?更具体地说,为什么它是 NilClass,为什么找不到 #inspect?

来自我的schema.rb

 create_table "locations", force: :cascade do |t|
    t.integer   "locatable_id"
    t.string    "locatable_type"
    t.datetime  "created_at",                                                              null: false
    t.datetime  "updated_at",                                                              null: false
    t.geography "coordinates",    limit: {:srid=>4326, :type=>"point", :geographic=>true}
  end

我正在使用 Rails 4.2.1.

从版本 3.0.0 开始,您无法设置列特定的地理工厂属性。 并且方法 set_rgeo_factory_for_column 被删除并弃用。 但是,您可以在整个应用程序范围内配置 RGeo 工厂。例如在你的初始值设定项中。

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  # By default, use the GEOS implementation for spatial columns.
  config.default = RGeo::Geos.factory_generator

  # But use a geographic implementation for point columns.
  config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point")
end