Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2012 22:02
Show Gist options
  • Save anonymous/2380428 to your computer and use it in GitHub Desktop.
Save anonymous/2380428 to your computer and use it in GitHub Desktop.
Sinatra + Mongo
require 'rubygems'
require 'sinatra/base'
require './models/contact'
require './models/phone'
require 'mongoid'
class Api < Sinatra::Base
before do
content_type :json
end
get '/contacts' do
Contact.all.to_json
end
post '/contacts' do
@contact = Contact.create(params[:contact])
@contact.to_json
end
end
require "mongoid"
class Contact
include Mongoid::Document
field :name, type: String
field :email, type: String
field :twitter, type: String
field :github, type: String
has_many :phones, :autosave => true
accepts_nested_attributes_for :phones
end
require 'mongoid'
class Phone
include Mongoid::Document
field :number, type: String
field :type, type: String
attr_accessible :number, :type
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment