Mongdb in rails 3 using mongo_mapper

Source: Internet
Author: User
Tags scalar

Using extends mapper with rails 3 is easier than ever. thanks to new features in activesupport, and the new activemodel framework (which extends mapper 0.9 + uses), your app can be up and running on MongoDB in a matter of seconds.

First, if you're 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 anotherORM), Simply openConfig/application. RbAnd 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 role Mapper to yourGemfile, And runBundle install:

 
Gem 'Mongo _ er'Gem 'Bson _ ext'

Now, you're almost ready to go, but you still need some configuration info. GenerateConfig/Mongo. ymlBy running:

Script/rails generate mongo_mapper: config

(Note: In versions of parameter mapper 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 MongoDBUri(I. e. On Heroku), then you can use the following settings for your production environment:

 
Production: Uri: <% = Env ['mongodb _ URI '] %>

Technically, you can initialize extends Mapper 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_mapperEnd

This will allow you to useRails generate ModelCommand with extends mapper.

You're now finished, go forth and create!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.