我尝试按照 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。