Problem creating more than one Rails app using MongoDB
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开发者_StackOverflow中文版 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!
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
精彩评论