2

I have a small issue with Rails and mongodb. I have a small app which works great in development mode. It also works in production mode, but when I enter the rails console I can't see anything in it.

user-001@marcus:/var/www/webapp% rails c RAILS_ENV="production"
You did not specify how you would like Rails to report deprecation notices for your       RAILS_ENV=production environment, please set config.active_support.deprecation to :log,    :notify or :stderr at config/environments/RAILS_ENV=production.rb
Loading RAILS_ENV=production environment (Rails 3.2.1)
1.9.3p0 :001 > User.all
=> [] 

my config/initialize/mongo.rb looks like this

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "webapp-#{Rails.env}"

if defined?(PhusionPassenger)
     PhusionPassenger.on_event(:starting_worker_process) do |forked|
     MongoMapper.connection.connect if forked
end
end

but it also looks empty

 $ mongo localhost:27017/mail1up-production
 MongoDB shell version: 1.8.2
 Fri Feb 17 18:26:00 *** warning: spider monkey build without utf8 support.  consider        rebuilding with utf8 support
 connecting to: localhost:27017/webapp-production
 > db.mycollection.stats()
 { "errmsg" : "ns not found", "ok" : 0 }
 > 

Am I doing something wrong? Why can't I see the data in the database? How can I test it?

Markus
  • 2,526
  • 4
  • 28
  • 35

2 Answers2

7

I think it may be related to your query syntax. I just ran into the same thing where I attempted to use the normal ActiveRecord queries. I'm using MongoDB with Mongoid as my ORM.

This other question is related to yours, although it too is using Mongoid: mongoid-finders-not-working

I found success using the following for an "all" query:

User.all.to_a

Community
  • 1
  • 1
Joe
  • 3,352
  • 3
  • 20
  • 19
1

It seems like you need to run mmconsole.

You will then be dropped into irb with MongoMapper included and your database set to ‘mm-test’.

In your situation it may be that your mongo.rb initializer isn't respected and that you are connected to the mm-test database instead of "webapp-#{Rails.env}".

I got some of this information here

Tyler Brock
  • 29,626
  • 15
  • 79
  • 79