Practical Ruby Projects With Mongo Db
-
Upload
alex-sharp -
Category
Technology
-
view
10.003 -
download
0
Transcript of Practical Ruby Projects With Mongo Db
![Page 1: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/1.jpg)
Practical Ruby Projects with
![Page 2: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/2.jpg)
Who am I?
Alex Sharp
Amphibious Code Creature at OptimisDev
@ajsharp
alexjsharp.tumblr.com
![Page 3: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/3.jpg)
We’re going to talk about two things.
![Page 4: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/4.jpg)
1. Why/how MongoDB is practical
![Page 5: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/5.jpg)
2. A few examples of how to use MongoDB with Ruby
![Page 6: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/6.jpg)
So what’s all this “practical” talk?
![Page 7: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/7.jpg)
Imagine if databases were cars...
![Page 8: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/8.jpg)
Any takers for MySQL?
![Page 9: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/9.jpg)
RDBMS (i.e. mysql) =~
![Page 10: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/10.jpg)
In other words...
![Page 11: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/11.jpg)
MySQL is very reliable.
![Page 12: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/12.jpg)
But it may not be a speed demon.
![Page 13: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/13.jpg)
Guesses for MongoDB?
![Page 14: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/14.jpg)
MongoDB =~
![Page 15: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/15.jpg)
Mongo is reliable too, but they’re also go pretty damn
fast.
![Page 16: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/16.jpg)
The Practicality of MongoDB
![Page 17: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/17.jpg)
Objects weren’t made for SQL schemas
Simplifying schema design
![Page 18: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/18.jpg)
Objects weren’t meant to be “mapped” to a SQL schema
Simplifying schema design
![Page 19: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/19.jpg)
We’re forced to create “relationships” when what we really want is properties for our objects.
Simplifying schema design
![Page 20: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/20.jpg)
Non-relational databases are better suited for objects
Simplifying schema design
![Page 21: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/21.jpg)
Maps/hashes/associative arrays are arguably among the best object serialization formats
Simplifying schema design
![Page 22: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/22.jpg)
@alex = Person.new( :name => "alex", :friends => [Friend.new("Jim"), Friend.new("Bob")])
![Page 23: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/23.jpg)
<Person:0x10017d030 @name="alex", @friends= [#<Friend:0x10017d0a8 @name="Jim">, #<Friend:0x10017d058 @name="Bob">]>
Native ruby object
![Page 24: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/24.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
JSON Representation
![Page 25: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/25.jpg)
SQL Schema Representation# in a SQL schemapeople: - name friends: - name - friend_id
![Page 26: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/26.jpg)
Ruby -> JSON -> SQL
people: - name friends: - name - friend_id
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
<Person:0x10017d030 @name="alex", @friends= [#<Friend:0x10017d0a8 @name="Jim">, #<Friend:0x10017d058 @name="Bob">]>
Ruby
JSON
SQL
![Page 27: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/27.jpg)
Ruby -> JSON -> SQL
people: - name friends: - name - friend_id
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
<Person:0x10017d030 @name="alex", @friends= [#<Friend:0x10017d0a8 @name="Jim">, #<Friend:0x10017d058 @name="Bob">]>
Ruby
JSON
SQLFeels like we’re having to work too hard here
![Page 28: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/28.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
Mongo Representation
![Page 29: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/29.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
Mongo Representation
In Mongo, friends is an “embedded document”
![Page 30: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/30.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
Mongo Representation
Great for one-to-many associations
![Page 31: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/31.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
Mongo Representation
No JOINS required.
![Page 32: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/32.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
Mongo Representation
This is a good thing for scalability, because JOINS limit our ability to horizontally scale.
![Page 33: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/33.jpg)
@alex.to_json{ name: "alex", friends: [{ name: "Jim" }, { name: "Bob" }] }
Mongo Representation
But more importantly, this seems more intuitive than messing with foreign keys
![Page 34: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/34.jpg)
Ok, so what?
You’re probably thinking...
“Listen GUY, SQL isn’t that bad”
![Page 35: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/35.jpg)
Ok, so what?
True.
![Page 36: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/36.jpg)
Ok, so what?
SQL is actually really, really good at what it was designed to do.
![Page 37: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/37.jpg)
Ok, so what?
But SQL schemas are designed for storing and querying data, not necessarily modeling objects.
![Page 38: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/38.jpg)
Ok, so what?
It is called Structured Query Language.
![Page 39: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/39.jpg)
Ok, so what?
And to talk to these schemas in software, we use ORMs.
![Page 40: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/40.jpg)
Ok, so what?
ORM stands for Object Relational Mapper
![Page 41: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/41.jpg)
Ok, so what?
We need ORMs to bridge the gap between SQL and native objects (in our case, Ruby objects)
![Page 42: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/42.jpg)
Ok, so what?
I don’t want to map my objects to a schema designed for querying data.
![Page 43: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/43.jpg)
Ok, so what?
I want to store my objects in a datastore that was designed for storing objects
![Page 44: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/44.jpg)
Ok, so what?
Mongo is great for this b/c it stores objects (documents) as binary JSON
![Page 45: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/45.jpg)
Ok, so what?
And as we’ve seen, JSON is great for representing native objects
![Page 46: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/46.jpg)
Ok, so what?
This is important because when I’m writing an application, I don’t want to accomodate my objects to my datastore.
![Page 47: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/47.jpg)
Ok, so what?
I want something flexible that stays out of my way, but doesn’t sacrifice performance.
![Page 48: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/48.jpg)
Schema design for humans, not machines
i.e. document-oriented is awesome
schema-less is for adults
Pragmatic balance of performance and functionality
Speed/performance of mongo is super awesome
Scalability features are really powerful too
In a Nutshell
![Page 49: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/49.jpg)
Practical Projects
![Page 50: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/50.jpg)
Three Examples
1.Blogging Application
2.Accounting Application
3.Logging
![Page 51: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/51.jpg)
Blogging Application
![Page 52: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/52.jpg)
Blogging Application
Much easier to model with Mongo than a relational database
![Page 53: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/53.jpg)
Blogging Application
A post has an author
![Page 54: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/54.jpg)
Blogging Application
A post has an author
A post has many tags
![Page 55: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/55.jpg)
Blogging Application
A post has an author
A post has many tags
A post has many comments
![Page 56: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/56.jpg)
Blogging Application
A post has an author
A post has many tags
A post has many comments
Instead of JOINing separate tables,we can use embedded documents.
![Page 57: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/57.jpg)
require 'mongo'
conn = Mongo::Connection.new.db('bloggery')posts = conn.collection('posts')authors = conn.collection('authors')
![Page 58: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/58.jpg)
# returns a Mongo::ObjectID objectalex = authors.save :name => "Alex"post = posts.save( :title => 'Post title', :body => 'Massive potification...', :tags => ['laruby', 'omg', 'lolcats'], :comments => [ { :name => "Loudmouth McGee", :email => '[email protected]', :body => "Something really ranty..." } ], :author_id => alex)
![Page 59: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/59.jpg)
# returns a Mongo::ObjectID objectalex = authors.save :name => "Alex"post = posts.save( :title => 'Post title', :body => 'Massive potification...', :tags => ['laruby', 'omg', 'lolcats'], :comments => [ { :name => "Loudmouth McGee", :email => '[email protected]', :body => "Something really ranty..." } ], :author_id => alex)
Joins not necessary. Sweet.
![Page 60: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/60.jpg)
Logging with Capped Collections
![Page 61: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/61.jpg)
Fixed-sized, limited operation, auto age-out collections (kinda like memcached)
Fixed insertion order
Super fast (faster than normal writes)
Ideal for logging and caching
Capped collections
![Page 62: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/62.jpg)
This is awesome.
![Page 63: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/63.jpg)
Now we have logs we can query (and analyze)
![Page 64: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/64.jpg)
Can also be used for troubleshooting when things
go wrong
![Page 65: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/65.jpg)
Bunyan
Thin ruby layer around a Mongo capped collection
![Page 66: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/66.jpg)
Text
require 'bunyan'
Bunyan::Logger.configure do |c| c.database 'my_bunyan_db' c.collection 'development_log' # == 100.megabytes if using rails c.size 104857600 end
Bunyan::Logger.save( :request_method => 'get', :status_code => 200)
![Page 67: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/67.jpg)
A simple accounting application (maybe)
![Page 68: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/68.jpg)
The object model
ledger
transactions
entries
*
*
![Page 69: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/69.jpg)
The object model
# Credits Debits
1 { :account => “Cash”, :amount => 100.00 }
{ :account => “Notes Pay.”, :amount => 100.00 }
2 { :account => “A/R”, :amount => 25.00 }
{ :account => “Gross Revenue”, :amount => 25.00 }
Ledger Entries
{Transaction {
Transaction
![Page 70: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/70.jpg)
Object model summary
Each ledger transaction belongs to a ledger.
Each ledger transaction has two ledger entries which must balance.
![Page 71: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/71.jpg)
@credit_entry = LedgerEntry.new :account => "Cash", :amount => 100.00, :type => "credit"@debit_entry = LedgerEntry.new :account => "Notes Pay.", :amount => 100.00, :type => "debit"
@ledger_transaction = LedgerTransaction.new :ledger_id => 1, :ledger_entries => [@credit_entry, @debit_entry]
Object Model with ActiveRecord
![Page 72: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/72.jpg)
Object Model with Mongo@ledger_transaction = LedgerTransaction.new :ledger_id => 1, :ledger_entries => [ { :account => 'Cash', :type => "credit", :amount => 100.00 }, { :account => 'Notes Pay.', :type => "debit", :amount => 100.00 } ]
![Page 73: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/73.jpg)
Object Model with Mongo@ledger_transaction = LedgerTransaction.new :ledger_id => 1, :ledger_entries => [ { :account => 'Cash', :type => "credit", :amount => 100.00 }, { :account => 'Notes Pay.', :type => "debit", :amount => 100.00 } ]
This is the perfect case for embedded documents.
![Page 74: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/74.jpg)
Object Model with Mongo@ledger_transaction = LedgerTransaction.new :ledger_id => 1, :ledger_entries => [ { :account => 'Cash', :type => "credit", :amount => 100.00 }, { :account => 'Notes Pay.', :type => "debit", :amount => 100.00 } ]
We would never have a ledger entry w/o a transaction.
![Page 75: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/75.jpg)
Using mongo w/ Ruby
Ruby mongo driver
MongoMapper
MongoID
Many other ORM/ODM’s under active development
![Page 76: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/76.jpg)
MongoMapper
![Page 77: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/77.jpg)
MongoMapper
• MongoDB “ORM” developed by John Nunemaker
![Page 78: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/78.jpg)
MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
![Page 79: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/79.jpg)
MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
• Very similar syntax to DataMapper
![Page 80: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/80.jpg)
MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
• Very similar syntax to DataMapper
• Declarative rather than inheritance-based
![Page 81: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/81.jpg)
MongoMapper
• MongoDB “ORM” developed by John Nunemaker
• author of HttpParty
• Very similar syntax to DataMapper
• Declarative rather than inheritance-based
• Very easy to drop into rails
![Page 82: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/82.jpg)
class Post include MongoMapper::Document
belongs_to :author, :class_name => "User"
key :title, String, :required => true key :body, String key :author_id, Integer, :required => true key :published_at, Time key :published, Boolean, :default => false timestamps!
many :tagsend
MongoMapper
class Tag include MongoMapper::EmbeddedDocument
key :name, String, :required => trueend
![Page 83: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/83.jpg)
Questions?
![Page 84: Practical Ruby Projects With Mongo Db](https://reader036.fdocuments.in/reader036/viewer/2022081414/5555a8d1d8b42afe5d8b4653/html5/thumbnails/84.jpg)
Thanks!