【问题标题】:Mongoid and collectionsMongoid 和集合
【发布时间】:2012-04-20 17:40:03
【问题描述】:

我是第一次尝试配置和使用 mongoid。我将 mongoid.yml 配置文件设置为:

主机:本地主机

数据库:表

和我的代码:

  Mongoid.load!("/mongoid.yml") 
  class Data        
    include Mongoid::Document
    field :study, type: String
    field :nbc_id, type: String
    field :short_title, type: String
    field :source, type: String
    field :start_date, type: Date
  end

  puts Data.study

我不断收到错误:

NoMethodError at / undefined method `study' for Data:Class

我认为这是因为我没有指定集合名称“test”。但是,我找不到有关如何执行此操作的示例。我是在 .yml 文件中还是在代码中指定它。什么是正确的语法。谁能指出我正确的方向?

发送。

【问题讨论】:

    标签: mongodb mongoid


    【解决方案1】:

    根据 Mongoid 文档,“默认情况下,Mongoid 将文档存储在一个集合中,该集合是类名的复数形式。对于下面的 Person 类,将存储文档的集合将被命名为 people。” http://mongoid.org/docs/documents.html

    文档继续说明 Mongoid 使用称为 ActiveSupport::Inflector#classify 的方法来确定集合名称,并提供有关如何自己指定复数的说明。

    或者,您可以通过在类定义中包含“store_in”来指定类中的集合名称。

    class Data        
        include Mongoid::Document
        store_in :test
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      相关资源
      最近更新 更多