rails3 使用 mongoid的步骤

文刀乱谈 2012-05-15

mongodb官方的文档(http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started)是专门为"mongomapper"准备的。不适用于"mongoid",所以,大家不要看了。

参考mongoid的官方文档:http://mongoid.org/

1.在Gemfile中增加:

gem "mongoid", "~> 2.4"
  gem "bson_ext", "~> 1.5"

2.$railsgmongoid:config

会生成config/mongoid.yml,形如:

development:
  host: localhost
  database: miaomiao_cat_development

test:
  host: localhost
  database: miaomiao_cat_test

# set these environment variables on your prod server
production:
  host: localhost
  database: miaomiaocat_production

3.编辑config/application.rb

删掉:

require 'rails/all'

增加:

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+

4.把development.rb,test.rb中,下面2行去掉:

引用
config.active_record.mass_assignment_sanitizer = :strict

config.active_record.auto_explain_threshold_in_seconds=0.5

5.去掉spec_helper.rb中的:

引用
config.use_transactional_fixtures = true

(以下步骤就可以参考mongoid官方文档了)

6.建立任意一个model.注意这个include

class Apple
    include Mongoid::Document
    #...
  end

相关推荐