Mongomapper with Rails 3

These are some notes I have taken while creating my first Rails 3 application with MongoDB/Mongomapper as the back end.

After installing RVM, Ruby 1.9.2dev, and Rails 3.0.0.beta4, I followed the instructions here to update a bunch of files (assuming you are running MongoDB locally):

config/initializers/mongo.rb

MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "#myapp-#{Rails.env}"

if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect_to_master if forked
end
end

Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.0.0.beta4'

gem 'mongo_mapper'
gem 'rails3-generators'

config/application.rb
(only first 8 lines shown, not change to the rest of the file)

require File.expand_path('../boot', __FILE__)

#require "rails/all"

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"

Then, I went on to create a model, and to do that I used kristianmandrup's rails3-generators gem:

sudo gem install rails3-generators
rails generate model Book --skip-migration --orm=mongomapper

Edit this model to contain at least one MongoDB key:
class Book
include MongoMapper::Document

key :title, String
key :author, String

timestamps!

# Validations :::::::::::::::::::::::::::::::::::::::::::::::::::::
# validates_presence_of :attribute

# Assocations :::::::::::::::::::::::::::::::::::::::::::::::::::::
# belongs_to :model
# many :model
# one :model

# Callbacks :::::::::::::::::::::::::::::::::::::::::::::::::::::::
# before_create :your_model_method
# after_create :your_model_method
# before_update :your_model_method

end


We'll need a controller to access the Book model:
rails generate controller Book

This creates an empty controller file. Edit book_controller.rb to look like this:

class BookController < ApplicationController
def index
@books = Book.all
end
end

Create a view for this controller and action in app/views/book/index.erb:

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Books</h1>
<% @books.each do |book| %>
<div class="entry">
<h3><%= book.title %></h3>
</div>
<% end %>

Add the Book routes in the routes.rb file:

Inspector::Application.routes.draw do |map|
resources :books
end

Start the server to see if the application works so far:
rails server

Navigate your browser to http://localhost:3000/books/index. If you get a blank page with "Books" only displayed, then you are doing well, no worries. There are no errors, but also there are no records to display. The easiest/quickest way to add some books at this point is to use the console. Type "rails console", and add a couple of books:

book1 = Book.create(:title => "title 1", :author => "author 1")
=> true
book2 = Book.create(:title => "title 2", :author => "author 2")
=> true

Finally, make sure the rails server is running, and try to access http://localhost:3000/books. If all went well, the two entries we just made will be listed.

PS. Good resources on this subject:
http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
http://blog.bitzesty.com/mongodb-with-mongomapper-and-ruby-on-rails

About

Twitter