README に従って activerecord-postgis-adapter をインストールしようとしましたが、以下のエラーが発生し続けるため、モデル インスタンスを作成できません。
NoMethodError: nil:NilClass の未定義のメソッド `point'
私のさまざまなファイルは次のようになります。
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'
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"
レコードを取得しようとしたときにレコードを追加した後、次のようになります。
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 を使用しています。