Your SlideShare is downloading. ×
Rails Loves MongoDB
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Rails Loves MongoDB


Published on

Rails Loves MongoDB. A talk given on 11/21/2013 at the Erie Ruby group. Dives into MongoDB's capabilities and how to use it in a Rails application.

Rails Loves MongoDB. A talk given on 11/21/2013 at the Erie Ruby group. Dives into MongoDB's capabilities and how to use it in a Rails application.

Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Rails.loves?(:mongodb) => true A Peek Into Making Awesomely Dynamic Applications
  • 2. How Many Have Experience With Rails?
  • 3. How Many Have Experience With Databases?
  • 4. How Many Have Heard Of MongoDB?
  • 5. MongoDB Is... Document database (NoSql) (1) Data is stored as BSON Objects (looks exactly like JSON) Gives high performance, high availability, and easy scalability Gives you ultimate flexibility with features like... Sweet Data Types: Hashes and Arrays Allows you to nest documents Allows for dynamic document attributes (1) Despite the word “NoSql” you can still create a relational database with MongoDB
  • 6. MongoDB In Relational Terms MongoDB MySql Document Row Collection Table Database Database Index Index
  • 7. More MongoDB Knowledge Two types of relations: Embedded and Referenced No such thing as a “join”, this type of thing can be achieved with embedded documents and linking Has a built in aggregation framework and map reducing support. You still have 1-1, 1-n, n-n relations
  • 8. What A MongoDB Documents Look Like
  • 9. How Does Rails Talk To MongoDB? Mongoid - A Object-DocumentMapper(ODM) (1) (1) There are other ODM’s out there for MongoDB, MongoMapper for example. I feel Mongoid is far and away the best one. Has a supportive and active maintainer named Durran Jordan.
  • 10. Why Not Use ActiveRecord? ActiveRecord does not have built in support for MongoDB. Mongoid replaces it. However, it still uses ActiveModel. You still have all the validation goodness.
  • 11. Getting Started Install MongoDB...Kind of important Mac: 
 http:/ / Ubuntu:
 http:/ /
  • 12. Getting Started With Rails Generate a new Rails 4 Application
  • 13. Modify Your Gemfile Add the following to your gemfile, then bundle (1)(2) (1) Current public release of Mongoid doesn’t support Rails 4. To install Mongoid 4.0, point the gem to the Mongoid github project. ! (2) If your using Rails 3, use the public gem
  • 14. Generate A Mongoid Configuration File
  • 15. What mongoid.yml Looks Like
  • 16. All Setup. What Kind of App Should We Make? Let’s build an app that helps you track video game achievements that you completed.
  • 17. What Models Do We Need To Create? system: name, short_name game: title, description, released_at, completed_at achievement: title, description, points, multiplayer, completed_at
  • 18. Let’s Build A Model Use the Rails generator as normal.
  • 19. What The Model Looks Like By Default Clean model just includes Mongoid::Document to a class.
  • 20. Lets Add Some Fields In ActiveRecord you use migrations to add and remove fields from tables. In Mongoid you declare your fields right in the model class.
  • 21. How About Some Validation
  • 22. Lets Make Some Relations
  • 23. The Game Model
  • 24. The Achievement Model
  • 25. Lets Review What We Just Did Generated our models Added the fields Added validation Made the relations
  • 26. Let’s Have Some Fun in Console
  • 27. Create A System Validation works Document Created
  • 28. Create A Game For Our System
  • 29. How About Another Game
  • 30. Let’s Create An Achievement
  • 31. How About A Multiplayer Achievement
  • 32. So How Do These Look Stored In The Database?
  • 33. How Do The Embedded Documents Look?
  • 34. We Completed One Of Those Achievements, lets update it
  • 35. Lets Store A Dynamic Field
  • 36. Lets Review What We Just Did We created a system called “Xbox One”. We created a new game via our system document, automatically creating the relation to system. We created a new game, then created the relation to system. We can find documents by providing the document ID. Created a single and multiplayer achievement for a game. Took a look and how these are stored in the database. Updated one of the achievements as completed. Updated a document with a dynamic field.
  • 37. Let’s Try Some Queries
  • 38. How about all the games that are completed?
  • 39. How About All Games Who have Achievements With Points Greater than 10?
  • 40. How About Achievements In A Game With Points Bet ween 10 And 30
  • 41. Some More Advanced Things You Can Do Custom Validation Methods Callbacks Scoping GeoNear Queries Map Reducing Aggregation Obser vers
  • 42. Some Resources - More information about MongoDB - Read up on Rails 4 - Documentation for Mongoid
  • 43. Follow Me Github - BillWatts Twitter - @BillWatts
  • 44. Thank You! Get This Project @ http:/ / BillWatts/rails_loves_mongodb_talk
  • 45. Any Questions?