2012-04-20 63 views
1

我正在尝试首次配置和使用mongoid。我已经设置了mongoid.yml配置文件简称为:Mongoid和集合

主机:本地主机

数据库:表

和我的代码:

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

我认为这是因为我没有指定'测试'的集合名称。不过,我无法找到如何做到这一点的例子。我是否在.yml文件或代码中指定它。什么是正确的语法。任何人都可以将我指向正确的方向吗?

Tx。

回答

2

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

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

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

class Data   
    include Mongoid::Document 
    store_in :test 

希望这会有所帮助!

+0

Marc。 Tx非常多! – user1311034 2012-04-21 03:45:44