使用activerecord_postgis_adapter的NoMethodError

geo*_*boy 4 postgis ruby-on-rails rgeo

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

NoMethodError:nil的未定义方法"point":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
Run Code Online (Sandbox Code Playgroud)

的Gemfile

gem 'rgeo'
gem 'rgeo-activerecord'
gem 'activerecord-postgis-adapter'
Run Code Online (Sandbox Code Playgroud)

配置/ application.rb中

require 'rails/all'
#require 'active_record/connection_adapters/postgis_adapter/railtie'
#require "#{Rails.root}/lib/rgeo"
Run Code Online (Sandbox Code Playgroud)

配置/ database.yml的

development:
<<: *default
database: geoproject-api_development
adapter: postgis
schema_search_path: "public,postgis"
Run Code Online (Sandbox Code Playgroud)

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

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

想知道它为什么或如何解决它?更具体地说,为什么它是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
Run Code Online (Sandbox Code Playgroud)

我正在使用Rails 4.2.1.

bob*_*bob 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
Run Code Online (Sandbox Code Playgroud)

  • 好。在另一种情况下,我遇到了相同的错误。目前,我的调查表明,如果我将rgeo初始值设定项更改为```RGeo :: ActiveRecord :: SpatialFactoryStore.instance.tap,请执行| config | config.default = RGeo :: Geographic.spherical_factory(srid:4326)end```一切正常。它与工厂的选择方式有某种联系。但就我而言,我只需要地理工厂。所以对我有用 (2认同)