Skip to content

Instantly share code, notes, and snippets.

@davidrichards
Created August 2, 2012 21:14
Show Gist options
  • Save davidrichards/3240691 to your computer and use it in GitHub Desktop.
Save davidrichards/3240691 to your computer and use it in GitHub Desktop.
A basic sinatra example
require 'sinatra'
require 'json'
require_relative './database.rb'
get '/accounts' do
content_type :json
begin
@user = User.find(params[:user_id])
{:accounts => @user.accounts.map(&:attributes)}.to_json
rescue ActiveRecord::RecordNotFound => e
[].to_json
end
end
require 'sinatra'
require 'sinatra/activerecord'
set :database, 'sqlite:///db/gateway.db'
class User < ActiveRecord::Base
has_many :accounts
def full_name
[first_name, last_name].compact.join(" ")
end
end
class Account < ActiveRecord::Base
belongs_to :user
end
require_relative './database'
require 'sinatra/activerecord/rake'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment