首页 > 解决方案 > Rails PostGIS ActiveRecord 适配器有时返回字符串而不是 RGeo::Geos::CAPIPointImpl 对象

问题描述

PostGIS ActiveRecord Adapter在 Rails API 应用程序中使用。我有一个名为的数据库表events,其中一列是 PostGIS 几何列,coordinates例如:

create_table 'events' do |t|
  t.geometry 'coordinates;
end

当用户查询我的 API 时,我使用Netflix's Fast JSON API序列化我的响应:

class EventSerializer
  include FastJsonapi::ObjectSerializer

    attribute :coordinates do |object|
      # coincidentally my column name and PostGIS Adapters method for getting the coordinates is the same 
      object.coordinates.coordinates
    end
end

现在大部分时间上述工作,我得到一个有坐标的响应,但时不时我会收到以下错误:

'undefined method coordinates for "0101000020E610000061C3D32B65965DC03657CD7344F64040" String, did you mean codepoints' 

上述情况偶尔发生,我无法重新创建它,因为大多数情况下一切正常。很好,我的意思是我在数据库中只有 5 条记录,如果执行最终序列化 5 条记录的相同函数,它将工作 50 次,但随后 1 次它会给我上面的错误并继续给我上面的错误后续调用。然后我重新启动服务器,一切恢复正常。

我最好的猜测是,序列化程序处理对象的速度比适配器解析 RGeo 对象以返回坐标的速度要快,但我不知道如何解决这个问题。

任何人有任何解决方案?

标签: ruby-on-railsactiverecordpostgis

解决方案


推荐阅读