mongoid 未初始化常量点

mongoid uninitialized constant Point

我听说并尝试实现这些库 mongoid-geospatial

但他们都提到这个 class Point 这对我来说是未定义的。我错过了什么?

我正在使用 ActiveSupport::Concern

向我的模型添加地理问题
module Mappable
  extend ActiveSupport::Concern

  included do
    include Mongoid::Geospatial

    field :coordinates, type: Point, spatial: true
    spatial_scope :coordinates

uninitialized constant Mappable::Point (NameError)

您需要从 proper namespace 中解决它。

field :coordinates, type: ::Mongoid::Geospatial::Point, spatial: true

文档中的示例有效,因为它们声明的 class 不会创建新的模块嵌套。但是,在您的情况下,即使您包含了 Mongoid::Geospatial.

,Ruby 也会尝试从当前模块 (Mapping) 解析 Point