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. 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 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. yml
By 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 Model
Command with extends mapper.
You're now finished, go forth and create!