Failed to recognize type of 'location'. It

2019-07-15 03:58发布

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..

2条回答
干净又极端
2楼-- · 2019-07-15 04:38

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.

查看更多
老娘就宠你
3楼-- · 2019-07-15 04:50

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.

查看更多
登录 后发表回答