Reputation: 3224
I want to create my first web application in rails with mongodb but I am having some troubles with proper integration. Firstly, here is my database.yml file:
development:
adapter: mongodb
database: mongo_development
host: localhost
test:
adapter: mongodb
database: mongo_test
host: localhost
production:
adapter: mongodb
database: mongo_production
host: localhost
In one of several tutorials I saw a activerecord model equivalent - MongoMapper. For example my User model:
class User
include MongoMapper::Document
key :name, String
key :age, Integer
many :books
end
I also added mongodb.rb to config/initializers:
db_config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))
if db_config[Rails.env] &&
db_config[Rails.env]['adapter'] == 'mongodb'
mongo = db_config[Rails.env]
MongoMapper.connection = Mongo::Connection.new(mongo['hostname'])
MongoMapper.database = mongo['database']
end
I've created controller for the user:
class UsersController < ApplicationController
def index
user = User.new(:name => 'Brandon')
user.books.build(:name => 'Programming', :started => 10.years.ago)
user.save!
puts User.where(:name => 'Brandon').first
end
end
But I'm still getting an error :
Please install the mongodb adapter: `gem install activerecord-mongodb-adapter` (no such file to load -- active_record/connection_adapters/mongodb_adapter)
My installed gems:
abstract (1.0.0)
actionmailer (3.0.7)
actionpack (3.0.7)
activemodel (3.0.7)
activerecord (3.0.7)
activeresource (3.0.7)
activesupport (3.0.8, 3.0.7)
....
mongo (1.3.1)
mongo_mapper (0.9.1)
rails (3.0.7)
railties (3.0.7)
rake (0.9.2)
rubygems-update (1.8.5)
Can anyone help?
UPDATE
I removed mongodb.rb initializer, database.yml and used rails g mongo_mapper:config
.It created file mongo.yml:
defaults: &defaults
host: 127.0.0.1
port: 27017
development:
<<: *defaults
database: mongo_db_app_development
test:
<<: *defaults
database: mongo_db_app_tests
# set these environment variables on your prod server
production:
<<: *defaults
database: mongo_db_app
username: <%= ENV['MONGO_USERNAME'] %>
password: <%= ENV['MONGO_PASSWORD'] %>
But i got error: No such file or directory - /home/adm/NetBeansProjects/MongoDBApp/config/database.yml
So I copied a contents of mongo.yml file to database.yml and I got error:
ActiveRecord::AdapterNotSpecified
database configuration does not specify adapter
Upvotes: 2
Views: 3472
Reputation: 7951
I recently wrote an article to build rails 3.0 project on mongodb.Rails 3.0 and mongoDB
Hope this is useful..
Upvotes: 0
Reputation: 9529
I answered a different question before on integrating mongodb on top of another db in this question:
How to configure MongoMapper and ActiveRecord in same Ruby Rails Project
You can follow that and leave out the part that applies to ActiveRecord.
Upvotes: 1
Reputation: 9084
MongoMapper is a full replacement for ActiveRecord--it's not simply an adapter.
Run rails g mongo_mapper:config
and that will create a mongo.yml config file for MongoMapper and you won't need the mongodb.rb initializer. Also, you'll have to remove the adapter: mongodb
from your database.yml, otherwise ActiveRecord will keep looking for a non-existant mongodb adapter.
Upvotes: 3