rails3 使用 mongoid的步骤

访问量: 552

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. $ rails g mongoid: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 

订阅/RSS Feed

Subscribe

分类/category