MongoDB with MongoMapper and Ruby on Rails

I'm sure we've all heard the pros & cons of the NoSQL movement so these will not be covered here. I've been experimenting with a number of alternatives to RDBMS for a while such as CouchDB, TokyoCabinet, Redis and recently MongoDB.

MongoDB (from "humongous") is a scalable, high-performance, open source, schema-free, document-oriented database. Written in C++, MongoDB features:

MongoDB bridges the gap between key-value stores (which are fast and highly scalable) and traditional RDBMS (which provide structured schemas and powerful queries).

- from the MongoDB site.

The reason I've settled on MongoDB is (for me) it's been the most useful, more so than straight KV stores such as Redis and Tokyo. Technically CouchDB is very similar to MongoDB being a document store too, however MongoDB seems to 'gel better' (that's a technical term).

To be able to use this MongoDB goodness in my Rails app, I'm making use of the MongoMapper gem as it's the most popular one (github forks and watchers metric). There are other ORMs which you can use with MongoDB, Mongoid seems like a good alternative and the rest are documented here: http://www.mongodb.org/display/DOCS/Ruby+Language+Center.

Installation

Download and run mongod http://www.mongodb.org/display/DOCS/Downloads

Install the gems:

Configure your environment, and remove AR:

Add a config file for your database:

And add an initializer to setup MongoMapper and friends:

Getting stuff done

Now I know what you might be thinking, 'oh no not another ORM all my favorite gems won't work!', that is not the case! (*in some cases)

Usage

These Models are stripped down but you get the idea. Let say you're using Devise, then your User model could look like this:

And if that user had comments the model could look like:

These are just to help kick-start your app development, for more complete examples see the open source apps and http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails :

Open Source Ruby Applications using MongoDB (and MongoMapper)

NewsMonger - A simple social news application demonstrating MongoDB and Rails


Oupsnow - A bugtracker in Rails/MongoMapper
http://github.com/shingara/oupsnow/

Watchtower - An example app built with Sinatra, Mustache and MongoDB
http://github.com/kneath/watchtower/

Shapado - stackoverflow like question and answer site: 
http://gitorious.org/shapado/shapado

mmmblog - a blogging engine by the same guys as Shapado (http://blog.ricodigo.com/)
http://gitorious.org/mmmblog

More at http://wiki.github.com/jnunemaker/mongomapper/projects-using-mongomapper

Pro Tips

  • MongoMapper uses a fork of the validatable gem - which has some differences with AR validations
  • Denormalization is needed to reduce round-trips to the DB
  • Change how you think of your data models - this is closer to an object store
  • The finders have nifty options checkout http://wiki.github.com/jnunemaker/mongomapper/whats-new (0.5.7)
  • Don't be afraid to drop down and use to the Ruby MongoDB driver directly http://www.mongodb.org/display/DOCS/Ruby+Tutorial
  • Keep an eye out on the github commit feed as it's a bit of a moving target still

Summary
No migrations (yes!), storing arrays and hashes as attribute keys (sweet), and inplace updates (ftw). We're not going to use MongoDB to build transactional systems any time soon, but for the majority of web applications it's a perfect fit.


Bit Zesty - Ruby on Rails Development (and MongoDB) UK

Loading mentions Retweet
Filed under  //  development   mongodb   nosql   ruby on rails  
Comments (9)

Encrypting Data in your Ruby on Rails Application

Whilst searching for a gem/plug-in to help with storing encrypted notes in a Rails app, I found strongbox. It uses public key encryption to encrypt and store data on a ActiveRecord attribute - Spike has a great writeup on his blog. One limitation I encountered was that it only uses one key to encrypt the data, which wasn't going to work when I have multiple users, all storing private information.

491113684_ee82e217f1_m.jpg
So I wrote a wrapper around strongbox that I am releasing called Safe - a Rails engine for storing encrypted data. Safe handles the generating of the public and private keys and uses strongbox to encrypt and decrypt the data. It's still in early stages, and it doesn't support all of the options that strongbox does, but it is on the TODO list.

So let me know if you find it useful and don't forget to symlink the keys directory in your deployment scripts!


Bit Zesty - Ruby on Rails developers in London.
 

Loading mentions Retweet
Filed under  //  ruby on rails   security  
Comment (1)

Better XML parsing with Rails

We recently built a Ruby on Rails application that was receiving a large amount of XML. This as you can imagine was pretty CPU intensive, especially as Rails (ActiveSupport) uses REXML under the covers to parse the incoming requests.

I started to wonder if it was possible to use a different XML parsing library, for an overview of some of the available libraries and benchmarks see John Nunemaker's post about parsing xml with ruby and the rubyinside post ruby xml performance benchmarks.

I've used most of these libraries at some point or another for parsing XML and I was really impressed with Nokogiri, and I desperately wanted to use it in production for the Rails backed parsing. So I started to look at the Rails source and I noticed this commit http://github.com/rails/rails/commit/694998ee4fb8d257ba78424cab630846327a0889.

YES! All you have to do (besides installing Nokogiri on your server) is specify:

ActiveSupport::XmlMini.backend = 'Nokogiri'


In your production.rb environment and your done. The results speak for themselves, here is a snapshot of our CPU usage before and after, with the same load.

Loading mentions Retweet
Filed under  //  ruby on rails   xml  
Comments (0)

Web Development Terminology

While we are not big fans of technical jargon here at Bit Zesty, in some cases it is more helpful to use the correct term rather than rolling out a long and convoluted description of the thing in question each time. We use a few specific terms again and again throughout our site, so I have picked these out and expanded on them below to help new visitors understand what we do.

 

Web Application (webapp)

A web application is computer software which is accessible through the Internet. Social networks, e-commerce sites and wikis are all examples of web applications.

  • Web applications enable you to deliver and update software without having to distribute and install it.  This makes it easier to reach new users as well as allowing you to maintain the software to a standard which keeps existing users happy.
  • One big advantage of web applications is that they are not dependant on the user’s operating system. This means that you can cover a wider market without having to make changes to your software or release different versions.

Ruby on Rails (RoR)

Ruby on Rails is a framework for creating web applications using the Ruby programming language.

  • We choose to use Ruby on Rails because it enables us to build applications more rapidly than on other frameworks, which means that the client receives more features at a lower cost.
  • Another positive is that it encourages good development practices, resulting in higher quality code. This ensures your application is extendable and scalable.
  • By providing excellent support for automated testing, it allows for robust code, which reduces the amount of maintenance required.

Agile Development

Agile is a development methodology which embraces collaboration with customers, face-to-face communication, self-organizing teams, frequent delivery, and the ability to respond to changing requirements.

  • Agile enables you to re-prioritise or even introduce new features at any point. This allows us to react to changes in your business and thus ensure that application is tailored exactly to your needs.

Test Driven Development (TDD)

Test Driven Development is a technique wherein automated tests are created before the program code. This forces code to be written and improved upon until it passes the test.

  • Test Driven Development helps to eliminate the majority of bugs, ensuring you get software that is robust and properly functioning.

This list is by no means complete and I intend to keep updating it, particularly when we add new terminology to our website.

 

Bit Zesty is a Ruby on Rails Company in London

Loading mentions Retweet
Filed under  //  agile   development   ruby on rails  
Comment (1)