问题
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:
NoMethodError: undefined method `point' for nil:NilClass
Here are what my different files look like:
Location.rb (the model, updated code)
# == 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'
config/application.rb
require 'rails/all'
#require 'active_record/connection_adapters/postgis_adapter/railtie'
#require "#{Rails.root}/lib/rgeo"
config/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?
From my 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.
回答1:
As of version 3.0.0 you can't set column specific geo factory properties. And the method set_rgeo_factory_for_column is removed and deprecated. You can, however configure RGeo factory application wide. 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
来源:https://stackoverflow.com/questions/30834862/nomethoderror-using-activerecord-postgis-adapter