Created
November 12, 2009 14:59
-
-
Save jnunemaker/232953 to your computer and use it in GitHub Desktop.
mongo initializer to load config from database.yml, authenticate if needed and ensure indexes are created
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
development: &global_settings | |
database: textual_development | |
host: 127.0.0.1 | |
port: 27017 | |
test: | |
database: textual_test | |
<<: *global_settings | |
production: | |
host: hostname | |
database: databasename | |
username: username | |
password: password | |
<<: *global_settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config = YAML.load_file(Rails.root + 'config' + 'database.yml')[Rails.env] | |
MongoMapper.connection = Mongo::Connection.new(config['host'], config['port'], { | |
:logger => Rails.logger | |
}) | |
MongoMapper.database = config['database'] | |
if config['username'].present? | |
MongoMapper.database.authenticate(config['username'], config['password']) | |
end | |
Dir[Rails.root + 'app/models/**/*.rb'].each do |model_path| | |
File.basename(model_path, '.rb').classify.constantize | |
end | |
MongoMapper.ensure_indexes! | |
if defined?(PhusionPassenger) | |
PhusionPassenger.on_event(:starting_worker_process) do |forked| | |
# if using older than 0.6.5 of MM then you want database instead of connection | |
# MongoMapper.database.connect_to_master if forked | |
MongoMapper.connection.connect_to_master if forked | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was pointed out to me on the Google group that you don't have to parse the database.yml yourself either. This wil suffice:
Assigning the Rails logger in all environments has worked fine for me, too.