Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0680c15408
Fetching contributors…

Cannot retrieve contributors at this time

133 lines (103 sloc) 2.865 kb

Mongo

There are many ORMs out there for Mongo (or ODMs in this case). This page will go over just a few.

Require the Mongo gem in your app:

require 'rubygems'
require 'sinatra'
require 'mongo'

MongoMapper

require 'mongomapper'

Create the Model class

class Link
  include MongoMapper::Document
  key :title, String
  key :link, String
end

Create the route:

get '/' do
 @links = Link.all
 haml :links
end

(top)

Mongoid

require 'mongoid'

Create the Model class

class Link
  include Mongoid::Document
  field :title, :type => String
  field :link, :type => String
end

Create the route:

get '/' do
 @links = Link.all
 haml :links
end

(top)

Candy

require 'candy'

Create the Model class

class Link
  include Candy::Piece
end

class Links
  include Candy::Collection
  collects :link   # Declares the Mongo collection is 'Link'
end

Link.connection # => Defaults to localhost port 27017
Link.db         # => Defaults to your username, or 'candy' if unknown
Link.collection # => Defaults to the class name ('Link')

Create the route:

get '/' do
 @links = Links.all
 haml :links
end

(top)

Mongomatic

require 'mongomatic'

Create the Model class

class Link < Mongomatic::Base
  def validate
    self.errors.add "title", "blank" if self["title"].blank?
    self.errors.add "link",  "blank" if self["link"].blank?
  end
end

Create the route:

get '/' do
 @links = Link.all
 haml :links
end

def validate
  self.errors.add "name", "blank" if self["name"].blank?
  self.errors.add "email", "blank" if self["email"].blank?
  self.errors.add "address.zip", "blank" if (self["address"] || {})["zip"].blank?
end

(top)

MongoODM

require 'mongo_odm'

Create the Model class

class Link
  include MongoODM::Document
  field :title
  field :link
end

Create the route:

get '/' do
 @links = Link.find.to_a
 haml :links
end

(top)

Jump to Line
Something went wrong with that request. Please try again.