使用 MongoDB 创建多个 Rails 应用程序时出现问题

发布于 2024-09-08 20:41:48 字数 728 浏览 1 评论 0原文

我正确安装了 MongoDB 并让它在我的 OSX 上运行。我使用 MongDB 和 Rails3 创建的第一个应用程序的标题是“todo”。 关于railscasts的说明,我创建了一个文件(config/initializers/ mongo.rb)并添加了这一行:

MongoMapper.database = "todo-
#{Rails.env}"

据推测,这创建了出现在我的 /data/db/ 文件中的文件,标题为“todo- 当我在Rails中使用generate命令来创建 模型,数据已正确存储在该文件中。一切顺利,到此为止 观点。

现在的问题是,我似乎无法在 /data/db 文件中创建新文件 我使用 Rails 创建新应用程序。 (我认为)数据文件应该是 从初始化文件创建(例如:

MongoMapper.database = "newproject-
#{Rails.env}"

我添加到每个新应用程序中。但是它 不是。

这是我的 gemfIle (与我的第一个应用程序一起使用!:

require 'rubygems'
gem 'mongo', '1.0'
source 'http://gemcutter.org'

gem 'rails', '3.0.0.beta4'
gem "mongo_mapper"
gem 'bson_ext', '1.0' 

任何帮助将不胜感激!

I properly installed MongoDB and got it running on my OSX. The first app I created using MongDB and Rails3 was titled 'todo". Per the
instructions on railscasts, I created a file (config/initilializers/
mongo.rb) and added this line:

MongoMapper.database = "todo-
#{Rails.env}"

Presumably, this created the files that appeared in my /data/db/ file entitled "todo-
development". When I used the generate command in Rails to create the
models, the data was correctly stored in this file. All good, up to this
point.

The problem now is that I can't seem to create NEW files in the /data/db file when
I create new apps with Rails. (I think) the data file should be
created from the initializer file (ex:

MongoMapper.database = "newproject-
#{Rails.env}"

that I add to each new app. But it
is not.

Here's my gemfIle (that worked with my first app!:

require 'rubygems'
gem 'mongo', '1.0'
source 'http://gemcutter.org'

gem 'rails', '3.0.0.beta4'
gem "mongo_mapper"
gem 'bson_ext', '1.0' 

Any help would be appreciated!

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

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

发布评论

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

评论(1

陌路终见情 2024-09-15 20:41:48

最终在 Google 群组中的克里斯蒂安·曼德鲁普 (Kristian Mandrup) 的帮助下解决了这个问题。谢谢克里斯蒂安。我需要取消 application.rb 文件中 config.generator 的注释,并将 orm 从 active_record 更改为 mongo_mapper。 (顺便说一句,我之前尝试运行生成器时遇到的错误是“没有为所需选项'--orm'提供值。”)

更多信息请参见:http://www.viget.com/extend/rails-3-generators-hooks/

对于它的价值,我包括让 MongoDB 和 Rails 3 正常协同工作所需的整个过程。


在 OSX 上安装 MongoDB

$ sudo port install mongodb

创建数据目录:

$ sudo mkdir -p /data/db

设置数据目录的权限:

$sudo chown `id -u` /data/db

在终端中启动 Mongo:

$ mongod run

访问本地主机以验证 MongoDB 是否正在运行。 :

http://localhost:28017/

使用 Rails 3 创建新项目:

$ rails new projectname --skip-activerecord

将其添加到 gemfile:

require 'rubygems'
gem 'mongo', '1.0'
source 'http://gemcutter.org'
gem 'rails', '3.0.0.beta4'
gem "mongo_mapper"
gem 'bson_ext', '1.0'

取消注释(并修改)application.rb 文件中的这些行:

config.generators do |g|
    g.orm :mongo_mapper
end

创建 config/initializer/mongo.rb 文件:

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

创建 lib/tasks/mongo.rake 文件:

namespace :db do
  namespace :test do
    task :prepare do # Stub out for MongoDB
    end
  end
end

安装gems:

$bundle install

创建第一个模型:

$rails generate scaffold Product name:string --skip-migration

创建 models/product.rb 文件:

class Product
 include MongoMapper::Document
  key :name, string
end

Finally figured this out with the help Kristian Mandrup in a Google Group. Thanks Kristian. I needed to uncomment the config.generator in my application.rb file and change orm from active_record to mongo_mapper. (btw, the error I was getting before when trying to run the generator was ""No value provided for required options '--orm'.")

More here: http://www.viget.com/extend/rails-3-generators-hooks/

For what it's worth, I'm including the entire process that I needed to take in order to get MongoDB and Rails 3 working together properly.


Install MongoDB on OSX

$ sudo port install mongodb

Create a data directory:

$ sudo mkdir -p /data/db

Set permissions for data directory:

$sudo chown `id -u` /data/db

Start Mongo in Terminal:

$ mongod run

Visit local host to verify that MongoDB is running:

http://localhost:28017/

Create new project with Rails 3:

$ rails new projectname --skip-activerecord

Add this to the gemfile:

require 'rubygems'
gem 'mongo', '1.0'
source 'http://gemcutter.org'
gem 'rails', '3.0.0.beta4'
gem "mongo_mapper"
gem 'bson_ext', '1.0'

uncomment out (and modify) these lines in application.rb file:

config.generators do |g|
    g.orm :mongo_mapper
end

Create config/initializer/mongo.rb file:

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

Create a lib/tasks/mongo.rake file:

namespace :db do
  namespace :test do
    task :prepare do # Stub out for MongoDB
    end
  end
end

Install gems:

$bundle install

Create first model:

$rails generate scaffold Product name:string --skip-migration

Create models/product.rb file:

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