mongoid.yml在哪里?

时间:2013-05-28 12:30:06

标签: ruby mongodb mongoid

我刚安装了mongoid。我尝试了他们的示例代码,它提出了一个漂亮的错误,建议“仔细检查你的mongid.yml ......”所以我去mongoid.org阅读他们的介绍,where they say:“Mongoid配置可以通过一个mongoid.yml“。但我深究每个人都认为像我这样的菜鸟会知道mongoid.yml的天堂在哪里。当然,我可以find -name mongoid.yml,但这不是很奇怪吗?这是一种每个新的Mongoid用户必须经历的令人痛苦的事吗?

编辑:好的,这正是我从Mongoid网站上掏出来的。

require 'mongoid'

class Human
  include Mongoid::Document
  field :name, type: String
  embeds_many :interests
end

class Interest
  include Mongoid::Document
  field :content, type: String
  embedded_in :human
end

ccfu = Human.where( name: "John Doe" )
ccfu.interests.create( content: "criminal activity" )

1 个答案:

答案 0 :(得分:3)

如果您没有使用Ruby on Rails,可以将mongoid.yml放在任何地方,然后在代码中使用load!方法加载它:

Mongoid.load!('path/to/mongoid.yml', :development)

或者:

ENV['MONGOID_ENV'] = 'development'
Mongoid.load!('path/to/mongoid.yml')

在Ruby on Rails上,它位于config目录中。