Time for action — setting up Mongoid

Once we have a project created (just like we saw earlier), we can configure Mongoid as follows:

$ rails generate mongoid:config
create config/mongoid.yml

The next code listing is what the config/mongoid.yml looks like:

development:
host: localhost
database: sodibee_development
test:
host: localhost
database: sodibee_test
# set these environment variables on your prod server
production:
host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
# slaves:
# - host: slave1.local
# port: 27018
# - host: slave2.local
# port: 27019

There is no direct generator for Mongoid. Simply do the following:

class Author
include Mongoid::Document
end

Your Rails project should not load ActiveRecord (For Rails version less than 3.0).

Ensure the following:

  • Remove config/database.yml
  • Remove the following line from config/application.rb:
    require 'rails/all'
    
  • Add the following line in config/application.rb:
    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "active_resource/railtie"
    require "rails/test_unit/railtie"
    

For Rails 3.1.x and Rails 3.0.x to ensure that you do not load ActiveRecord.

Execute the following command:

$ rails new <project_name> -O skip-bundle

What just happened?

We set up Mongoid, which looks almost similar to MongoMapper. However, the Mongoid::Document and MongoMapper::Document differ considerably in the way they are structured internally.

MongoMapper::Document includes the various plugins as follows:

  • include Plugins::ActiveModel
  • include Plugins::Document
  • include Plugins::Querying
  • include Plugins::Associations
  • include Plugins::Caching
  • include Plugins::Clone
  • include Plugins::DynamicQuerying
  • include Plugins::Equality
  • include Plugins::Inspect
  • include Plugins::Indexes
  • include Plugins::Keys
  • include Plugins::Dirty
  • include Plugins::Logger
  • include Plugins::Modifiers
  • include Plugins::Pagination
  • include Plugins::Persistence
  • include Plugins::Accessible
  • include Plugins::Protected
  • include Plugins::Rails
  • include Plugins::Safe
  • include Plugins::Sci
  • include Plugins::Scopes
  • include Plugins::Serialization
  • include Plugins::Timestamps
  • include Plugins::Userstamps
  • include Plugins::Validations
  • include Plugins::EmbeddedCallbacks
  • include Plugins::Callbacks

Mongoid::Document includes these modules via Mongoid::Components as follows:

  • include ActiveModel::Conversion
  • include ActiveModel::MassAssignmentSecurity
  • include ActiveModel::Naming
  • include ActiveModel::Observing
  • include ActiveModel::Serializers::JSON
  • include ActiveModel::Serializers::Xml
  • include Mongoid::Atomic
  • include Mongoid::Attributes
  • include Mongoid::Collections
  • include Mongoid::Copyable
  • include Mongoid::DefaultScope
  • include Mongoid::Dirty
  • include Mongoid::Extras
  • include Mongoid::Fields
  • include Mongoid::Hierarchy
  • include Mongoid::Indexes
  • include Mongoid::Inspection
  • include Mongoid::JSON
  • include Mongoid::Keys
  • include Mongoid::Matchers
  • include Mongoid::NamedScope
  • include Mongoid::NestedAttributes
  • include Mongoid::Persistence
  • include Mongoid::Relations
  • include Mongoid::Safety
  • include Mongoid::Serialization
  • include Mongoid::Sharding
  • include Mongoid::State
  • include Mongoid::Validations
  • include Mongoid::Callbacks
  • include Mongoid::MultiDatabase

If we compare the modules, there is little to debate. Both have similar features but are implemented in different ways internally. The only way to understand them in detail is to dig into the code.

Note

Initially, I did wonder about why MongoMapper and Mongoid don't just merge like Rails and Merb. When I started digging into the code, I realized how different the internal implementation is. Do read this http://www.rubyinside.com/mongoid-vs-mongomapper-two-great-mongodb-libraries-for-ruby-3432.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.90.12