[Rails] wrong database created

82 views
Skip to first unread message

Adinda Praditya

unread,
Jan 2, 2011, 10:57:13 PM1/2/11
to mon...@googlegroups.com
Hi All,

I'm trying mongoid out on Rails 3.0.3 on my current personal project. I configured it based on the doc, generated the config file. I have mongod running locally and seed script (db/seeds.rb). So after running the rake task, I don't see any database as configured in mongoid.yml is created in mongo console. Instead, it created database named 'dbname'. This is quite annoying as it uses the same database for testing.

I don't know what else should I check so I hope this information is sufficient enough to see the problem. Thank you in advance.

Adinda

Ethan Gunderson

unread,
Jan 2, 2011, 11:05:00 PM1/2/11
to mon...@googlegroups.com
Can you show us what you have for a config file?

Adinda Praditya

unread,
Jan 2, 2011, 11:10:14 PM1/2/11
to mon...@googlegroups.com
It's from rails generate mongoid:config 

defaults: &defaults

  host: localhost

  # slaves:

  #   - host: slave1.local

  #     port: 27018

  #   - host: slave2.local

  #     port: 27019


development:

  <<: *defaults

  database: beri_development


test:

  <<: *defaults

  database: beri_test


# set these environment variables on your prod server

production:

  host: <%= ENV['MONGOID_HOST'] %>

  port: <%= ENV['MONGOID_PORT'] %>

  username: <%= ENV['MONGOID_USERNAME'] %>

  password: <%= ENV['MONGOID_PASSWORD'] %>

  database: <%= ENV['MONGOID_DATABASE'] %>

Ethan Gunderson

unread,
Jan 2, 2011, 11:20:56 PM1/2/11
to mon...@googlegroups.com
I haven't checked the implementation to see if it matters, but the database names in my config file are strings, which is not what gets generated.

Adinda Praditya

unread,
Jan 2, 2011, 11:36:36 PM1/2/11
to mon...@googlegroups.com
Basically it should not be a problem as long as it generates different databases for each environments. For additional information, here's the first lines in my db/seeds.rb

Mongoid.master.collections.reject { |c| c.name == 'system.indexes'}.each(&:drop)


On the rails console:

ruby-1.8.7-p299 > Mongoid.master.name
 => "dbname"

And here's the mongo console

$ mongo                                                                                       ~ <12:09:30>
MongoDB shell version: 1.6.5
connecting to: test
> show dbs
admin
dbname
local
rails3_mongoid_devise_development
test

Ethan Gunderson

unread,
Jan 2, 2011, 11:54:33 PM1/2/11
to mon...@googlegroups.com
What version of Mongoid are you using? I'm not able to reproduce this locally, so any other info you can give would be helpful.

Adinda Praditya

unread,
Jan 3, 2011, 12:09:03 AM1/3/11
to mon...@googlegroups.com
I found the problem. I was checking the configuration and found this in config/initializers/mongoid.rb that explains everything.

settings = URI.parse(ENV['MONGOHQ_URL'] || 'mongodb://localhost/dbname')

database_name = settings.path.gsub(/^\//, '')


Mongoid.configure do |config|

  config.master = Mongo::Connection.new(settings.host, 

settings.port).db(database_name)

  config.master.authenticate(settings.user, settings.password) if settings.user

end


It's based on https://github.com/fortuity/rails3-mongoid-devise/wiki/Tutorial-(Walkthrough). I commented every line in the file, ran rake db:seed and it generates the expected databases. It turns out the file is not needed after all. I'll should send a note about this.

Ethan, thank you for responding very quickly.


Adinda

Daniel Kehoe

unread,
Jan 10, 2011, 6:13:46 PM1/10/11
to Mongoid
Adinda Praditya,

Could you please create an issue at
https://github.com/fortuity/rails3-mongoid-devise/issues

I'd like to update the example app so other people don't have this
problem.

If you can provide details about what to fix, it will be helpful.

-- Daniel

Adinda Praditya

unread,
Jan 10, 2011, 7:31:20 PM1/10/11
to mon...@googlegroups.com
Daniel,

Since there's no config/initializers/mongoid.rb on the repo, there's no fix. So I just updated the wiki page removing the suggestion to use the config file as it will create the same database name for all rails environments. I hope this would be enough.

Adinda
Reply all
Reply to author
Forward
0 new messages