【问题标题】:Rails console and mongodbRails 控制台和 mongodb
【发布时间】:2012-03-09 02:54:12
【问题描述】:

我对 Rails 和 mongodb 有一个小问题。我有一个在开发模式下运行良好的小应用程序。它也可以在生产模式下工作,但是当我进入 rails 控制台时,我什么也看不到。

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
=> [] 

我的 config/initialize/mongo.rb 看起来像这样

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

但它看起来也很空旷

 $ 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 }
 > 

我做错了吗?为什么我看不到数据库中的数据?如何测试?

【问题讨论】:

  • 你发现问题了吗?

标签: ruby-on-rails ruby-on-rails-3 mongodb mongomapper


【解决方案1】:

您似乎需要运行mmconsole

然后,您将被放入包含 MongoMapper 的 irb 中,并且您的数据库设置为“mm-test”。

在您的情况下,可能是您的 mongo.rb 初始化程序没有受到尊重,并且您连接到 mm-test 数据库而不是“webapp-#{Rails.env}”。

我得到了一些这样的信息here

【讨论】:

    【解决方案2】:

    我认为这可能与您的查询语法有关。我在尝试使用普通 ActiveRecord 查询时遇到了同样的问题。我使用 MongoDB 和 Mongoid 作为我的 ORM。

    另一个问题与您的问题有关,尽管它也使用 Mongoid: mongoid-finders-not-working

    我发现使用以下“全部”查询成功:

    User.all.to_a

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 2016-06-30
      相关资源
      最近更新 更多