使用 Yaml 进行 MongoMapper 配置 [英] Use Yaml for MongoMapper Config
本文介绍了使用 Yaml 进行 MongoMapper 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的 Yaml 配置文件,mongo.yml
:
I have my Yaml configuration file, mongo.yml
:
development:
adapter: mongodb
database: fhsclock_development
host: localhost
port: nil
test:
adapter: mongodb
database: fhsclock_test
host: localhost
port: nil
production:
adapter: mongodb
database: fhsclock
hosts:
- - localhost
- nil
- - staff.mongohq.com
- 10015
如何使用此文件进行配置和与 MongoMapper 的连接?
How do I use this file for configuration and connection with MongoMapper?
推荐答案
MongoMapper 只会使用该文件,前提是您使用的是 Rails 并且该文件位于 config/mongo.yml
.如果你不在 Rails 上,你可以适应 this codenoreferr从源头:
MongoMapper will just use the file if it's you're using Rails and the file is at config/mongo.yml
. If you're not on Rails, you can adapt this code from the source:
config_file = Rails.root.join('config/mongo.yml')
if config_file.file?
config = YAML.load(ERB.new(config_file.read).result)
MongoMapper.setup(config, Rails.env, :logger => Rails.logger)
end
此外,您文件中的适配器"是无关紧要的.(请参阅入门文档).来自 rails g mongo_mapper:config
的 mongo.yml
看起来像:
Also, the "adapter" in your file is extraneous. (See the Getting Started documentation). A mongo.yml
from rails g mongo_mapper:config
looks like:
defaults: &defaults
host: 127.0.0.1
port: 27017
development:
<<: *defaults
database: my_app_development
test:
<<: *defaults
database: my_app_test
# set these environment variables on your prod server
production:
<<: *defaults
database: my_app
username: <%= ENV['MONGO_USERNAME'] %>
password: <%= ENV['MONGO_PASSWORD'] %>
这篇关于使用 Yaml 进行 MongoMapper 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文