我尝试按照README安装activerecord-postgis-adapter但无法创建模型实例,因为我不断收到下面提到的错误:
NoMethodError:未定义的方法`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
的Gemfile
gem 'rgeo'
gem 'rgeo-activerecord'
gem 'activerecord-postgis-adapter'
配置/ application.rb中
require 'rails/all'
#require 'active_record/connection_adapters/postgis_adapter/railtie'
#require "#{Rails.root}/lib/rgeo"
配置/ 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。
答案 0 :(得分:9)
从版本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