Skip to content

Instantly share code, notes, and snippets.

@louissalin
Created June 4, 2014 00:09
Show Gist options
  • Save louissalin/479093b5e3a34248b09a to your computer and use it in GitHub Desktop.
Save louissalin/479093b5e3a34248b09a to your computer and use it in GitHub Desktop.
Active Record code that may or may not work
require 'sinatra'
require 'active_record'
require 'sqlite3'
require 'json'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'app.db'
)
class User < ActiveRecord::Base
has_many :notes
validate_presence_of :name
end
class Note < ActiveRecord::Base
belongs_to :user
end
get '/' do
User.create(id: 1)
user = User.first
puts User.count
puts User.find(1).inspect
"user name: #{User.first.name}"
end
get '/users/:id' do
User.find(params[:id]).toJson
end
post '/users/:id/notes' do
user_id = params[:id]
note = params[:note]
Notes.create(note.merge(user_id: user_id))
end
get '/users/:id/notes' do
user = User.find(params[:id]).include(:notes)
user.notes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment