将Yaml用于MongoMapper配置

时间:2011-10-26 00:11:15

标签: yaml mongomapper

我有我的Yaml配置文件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?

1 个答案:

答案 0 :(得分:9)

如果您正在使用Rails且文件位于config/mongo.yml,MongoMapper将只使用该文件。如果您不在Rails上,则可以调整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

此外,文件中的“适配器”是无关紧要的。 (见Getting Started documentation)。来自mongo.yml的{​​{1}}看起来像:

rails g mongo_mapper:config
相关问题