MongDB in Rails 3 using mongo_mapper

Using MongoMapper with Rails 3 is easier than ever. Thanks to new features in ActiveSupport, and the new ActiveModel framework (which MongoMapper 0.9+ uses), your app can be up and running on MongoDB in a matter of seconds.

First, if you’re generating a new Rails 3 application, it is recommended to leave out the ActiveRecord dependencies (unless you need them of course). From the console, just run:

rails new my_app --skip-active-record

But, not everyone is starting fresh. If you’re converting an existing Rails 3 application from ActiveRecord (or another ORM), simply openconfig/application.rb and replace

require 'rails/all'

with

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# Uncomment for asset pipelining in Rails 3.1
# require "sprockets/railtie"

Next, add MongoMapper to your Gemfile, and run bundle install:

gem 'mongo_mapper'
gem 'bson_ext'

Now, you’re almost ready to go, but you still need some configuration info. Generate config/mongo.yml by running:

script/rails generate mongo_mapper:config

(Note: in versions of MongoMapper below 0.9+, you were required to implement any configuration files manually using an initializer. This is now resolved, and requires nothing more on your part.

If you want to configure your application with a MongoDB URI (i.e. on Heroku), then you can use the following settings for your production environment:

production:
 uri: <%= ENV['MONGODB_URI'] %>

Technically, you can initialize MongoMapper and use it to store data now. However, I like to configure Rails’ model generator. Inside of the Application class (config/application.rb) I add:

config.generators do |g|
  g.orm :mongo_mapper
end

This will allow you to use the rails generate model command with MongoMapper.

You’re now finished, go forth and create!

转载于:https://www.cnblogs.com/feichan/archive/2012/10/26/2741446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值