Rails 控制台和 mongodb

发布于 2025-01-06 20:42:17 字数 1273 浏览 1 评论 0原文

我有一个关于 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 }
 > 

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2025-01-13 20:42:17

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

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

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

User.all.to_a

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

那伤。 2025-01-13 20:42:17

看来您需要运行 mmconsole

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

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

我在此处获得了一些信息

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文