Failed to recognize type of 'location'. It

2019-07-15 04:12发布

问题:

I'm trying to use the gem activerecord-postgis-adapter and when i try to execute this code :

require 'active-record' 
require 'pg'
require 'active record-postgis-adapter'

class Place < ActiveRecord::Base
   record = Place.find(1)
   line = record.location
   puts line.srid
end

srid is a method of activerecord-postgis-adapter and here i'm trying to display the srid of my geographical column (location) but the console keeps telling me Failed to recognize type of 'location'. It will be treated as String..

回答1:

Probably found a solution to my problem, i used postgresql as the adapter when connecting to the database, i changed to postgis and now it works.



回答2:

1)Your example has a space in "active record".

Use require 'activerecord-postgis-adapter'.

2) As mentioned in other answer: change postgresql for postgis:

In project_name/config/database.yml, change the adapter:

 default: &default
  adapter: postgresql

Becomes

 default: &default
  adapter: postgis

3) Make sure you have the postgis extension enabled psql -d your_project_name_development -tAc "SELECT 1 from pg_extension where extname='postgis'" | grep -q 1 || \ psql -d your_project_name_development --command="CREATE EXTENSION postgis"

Also, in my project, I had to reimport the data.

Note: you might also need require 'rgeo-activerecord' if you're using the RGeo gem.