6

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

NoMethodError: 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'

配置/应用程序.rb

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

配置/数据库.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。

4

1 回答 1

10

从 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
于 2015-06-15T17:36:39.190 回答