【问题标题】:Queries return incorrect values with multi-level single table inheritance using Mongoid使用 Mongoid 的多级单表继承查询返回不正确的值
【发布时间】:2016-01-18 04:49:49
【问题描述】:

在我的项目中,我有以下类层次结构,使用 Mongoid 和 STI:

class User
  include Mongoid::Document
end

class SpecificUser < User
end

class MoreSpecificUser < SpecificUser
end

当我查询 User.countMoreSpecificUser.count 时,Mongoid 给了我正确的结果,但返回 0 当我尝试SpecificUser.count。只有在我查询MoreSpecificUser.count 之后它才会返回正确的值 为SpecificUser.count。有没有办法让 SpecificUser 中的查询返回正确的值 之前没有查询过它的子类?

注意:它之前与 MongoMapper 一起工作,只是在我迁移到 Mongoid 后才崩溃。

我的 mongoid.yml:

development:
  clients:
    default:
      database: dev_project
      hosts:
        - 127.0.0.1:27017
      options:
        preload_models: true
        max_pool_size: 16
  options:
    raise_not_found_error: false

我正在使用 Mongoid 5、MongoDB 2.6、Ruby 2.2.2 和 Rails 4.2.4。

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid


    【解决方案1】:

    似乎在 application.rb 中添加config.mongoid.preload_models = true 可以解决它。

    http://docs.mongodb.org/ecosystem/tutorial/ruby-mongoid-tutorial/#model-preloading

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多