简体   繁体   English

使用activerecord_postgis_adapter的NoMethodError

[英]NoMethodError using activerecord_postgis_adapter

I tried installing activerecord-postgis-adapter as per the README but am unable to create the model instance as I keep getting the error mentioned below: 我尝试按照自述文件安装activerecord-postgis-adapter但无法创建模型实例,因为我不断收到下面提到的错误:

NoMethodError: undefined method `point' for nil:NilClass NoMethodError:nil的未定义方法“point”:NilClass

Here are what my different files look like: 以下是我的不同文件的样子:

Location.rb (the model, updated code) 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 的Gemfile

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

config/application.rb 配置/ application.rb中

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

config/database.yml 配置/ database.yml的

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

After adding a record when I try to retrieve it, I get the following: 在我尝试检索记录时添加记录后,我得到以下内容:

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

Wondering why that it is or how can I fix it? 想知道它为什么或如何解决它? More specifically, why is it a NilClass and why is the #inspect not being found? 更具体地说,为什么它是NilClass,为什么没有找到#inspect?

From my schema.rb 来自我的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

I am using Rails 4.2.1. 我正在使用Rails 4.2.1。

As of version 3.0.0 you can't set column specific geo factory properties. 从版本3.0.0开始,您无法设置列特定的地理工厂属性。 And the method set_rgeo_factory_for_column is removed and deprecated. 并删除并弃用方法set_rgeo_factory_for_column。 You can, however configure RGeo factory application wide. 但是,您可以配置RGeo工厂应用程序范围。 For example in your initializer. 例如,在初始化程序中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 activerecord_postgis_adapter:nil的未定义方法`point&#39;:NilClass - activerecord_postgis_adapter: undefined method `point' for nil:NilClass PostGIS ActiveRecord适配器不起作用 - PostGIS ActiveRecord Adapter not works 使用 activerecord-postgis-adapter 保存点值 - Saving a point value using activerecord-postgis-adapter 使用gem postgis_adapter - Using gem postgis_adapter activerecord-postgis-adapter:# <ArgumentError: wrong number of arguments (3 for 4)> - activerecord-postgis-adapter: #<ArgumentError: wrong number of arguments (3 for 4)> 在任何来源中都找不到activerecord-postgis-adapter-5.0.0 - Could not find activerecord-postgis-adapter-5.0.0 in any of the sources 麻烦用activerecord-postgis-adapter添加st_point列 - Trouble adding st_point column with activerecord-postgis-adapter 尝试使用activerecord-postgis-adapter构建数据库时使用undefined方法 - undefined method when trying to build db with activerecord-postgis-adapter NameError: 未初始化常量 ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDefinition 与 gem activerecord-postgis-adapter - NameError: uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDefinition with gem activerecord-postgis-adapter activerecord-postgis-adapter v2不会在Rails 4中创建单独的模式 - activerecord-postgis-adapter v2 do not create separate schema in Rails 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM