7

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
geoboy
  • 1,172
  • 1
  • 11
  • 25

1 Answers1

11

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
bob
  • 126
  • 1
  • 3
  • Thanks so much, that worked! I have updated the question with an updated problem, if you could take a look! Really appreciate it! – geoboy Jun 16 '15 at 01:26
  • Unfortunately I don't see the full code of your class and all the rails application. Are you sure you run ````rails c```` when starting the interactive console? – bob Jun 16 '15 at 08:27
  • Yes, I am using rails c command. I've updated the model code above in the comment - let me know if that helps! Thanks again! – geoboy Jun 16 '15 at 16:41
  • 3
    Ok. I got the same error in another situation. And currently my investigation shows that if I change rgeo initializer to ```RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config| config.default = RGeo::Geographic.spherical_factory(srid: 4326) end``` everything works. It's somehow connected with the way the factory is chosen. But in my case I only need the geographic factory. So it worked for me – bob Jun 26 '15 at 17:02
  • Interesting, to make sure I understand correctly - this is instead of the initializer code that was suggested before? – geoboy Jun 29 '15 at 21:46
  • Yes. That's correct. You just have to remove the custom registered Geographic spherical factory and make it default (that was my case as the Geos factory wasn't used). My debugging showed that there were problems using custom factory for geo_type. I'm in the process of further investigation, as I'd be happy to find the solution and maybe create a pull request for that issue – bob Jun 30 '15 at 08:48
  • I also added `RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config| config.default = RGeo::Geographic.spherical_factory(srid: 4326) end` to `config/initializers/rgeo.rb` and everything is working fine now. – Avishai Jun 10 '19 at 22:06