蓝天,小湖,湖水中一方小筑

Playing MongoDB with Rails

This post will try to implement a web app used for recording mobile phone of cheaters. This idea is came from cooler@linuxfb, and I have used this for a presentation about GAE on 2009, and here is the slide.

NOTE: Code still under developing, the Git repo is shown at the end of article.

Creating Rails project has been talked in previous post, so this post will focus on others.

MongoDB

Thought I have heard MongoDB before, I just start to use it recently. It is a document based NoSQL database, and it is developer friendly. Detailed information can be get from its website.

  1. Install MongoDB on MacOS

On MacOS, the MongoDB can be installed via homebrew

brew install mongodb

The MongoDB will be installed if no error occurred. Now we should configure the MongoDB started up while system starts up.

  1. Install driver

For using MongoDB with Ruby, the gem mongo and bson_ext is needed, the second gem is used to improve performance. The gem mongo is the driver for MongoDB while bson_ext is used to improve performance.

gem install mongo gem install bson_ext

  1. Install Mongoid gem

This gem can be used to provide object mapper to the Rails, which can provide more useful features such as validations, associations and other high- level data modeling functions. Like other gems, you can install Mongoid via gem easily.

gem install mongoid

Besides Mongoid, there are also some other mappers, which can be linked from the doucment page of MongoDB.

After those steps done, the system is ready for playing MongoDB with Rails, let’s go.

Web App

Prepare the Rails Gem

After creating the Rails application with --skip-active-record option, the Gemfile should also be updated to include MongoDB related gems. Here is the new added lines in the Gemfile.

gem 'mongo'
gem 'bson_ext'
gem "mongoid", "~> 3.0.0.rc"

After adding those lines into Gemfile, remember to run bundle install for checking the dependencies.

Generate Configuration File for mongoid

The configuration file of mongoid can be generated with command rails g mongoid:config, then the file config/mongoid.yml will be generated. The generated file has some options and plenty of comments, so it is easy to modify it as requirements.

The Web App

The Model

For fast developing, we are using scaffold here. This web app should save two type of data, one is cheater and the other is provider. The Cheaters collection will save the information about cheaters, which contains the mobile phone number to identify, the description , and how many votes have been applied to this record. The Providers collection will save E-mail address of provider and how many mobile phones have been submitted. Here is the command:

rails g scaffold cheater mobile:string desc:string vote_up:integer vote_down:integer -T -O
rails g scaffold provider email:string -T -O

The generator will generate the model with mongoid module, instead of ActiveRecord module. The Cheaters and Providers should have many-to- many relationship, which is specified by keyword has_and_belongs_to_many, so the generated model file should be updated to this:

class Cheater
  include Mongoid::Document
  field :mobile, type: String
  field :desc, type: String
  field :vote_up, type: Integer
  field :vote_down, type: Integer

  has_and_belongs_to_many :providers
end


class Provider
  include Mongoid::Document
  field :email, type: String

  has_and_belongs_to_many :cheaters
end

Do not like the SQL database, there is no migration needed for MongoDB, which is more convenient during the development and upgrade.

The Controller and Viewers

The scaffold generated contains controllers, views and URL mapping, now we are going to go though the controller. The file config/routers.rb dispatch the URL mapping to the model controller by using resources keyword, and the server will response to URL /providers and /cheaters. The generated view can perform basic operations to the data. The prime tasks here is complete the basic operations of data and create a index page about this application.

I plan to use the controller generated as scaffold only sending and receiving JSON data, so a new controller named smedia (means static media, I prefer this name for this) will be created.

rails g controller smedia -T -O

The root path will be set to the smedia page after the controller generated. Remember to remove the public/index.html before enabling the root routings.

In the index page, there will be two blocks. One block is used to display the cheaters ordered by top positive vote, and the other block is used for submitting new cheater information. Gem twitter-bootstrap- rails will be used to beautify the web page. As mentioned in the document page, those two commands can be used to initialize the bootstrap framework:

rails g bootstrap:install
rails g bootstrap:layout application fixed

The first line will prepare the bootstrap assets, and the second is creating layout with bootstrap template. Please follow the document page for more detail.

The web page will show the cheater’s list by default, and there is a form to submit the cheater information. The vote field in Cheaters collection is used to record how many agreement or disagreement to this possible cheater. The detail implementation can be referred from the source code: https://github.com/hzmangel/cheater-recorder