Giter VIP home page Giter VIP logo

activerecordsinatra's Introduction

Up and Running with Sinatra and ActiveRecord

> mkdir activerecord-sinatra && cd activerecord-sinatra
> touch app.rb

Install two gems for ActiveRecord and the JSON gem for a real API-looking output.

> gem install activerecord sinatra-activerecord json

I will be using Postgres on my local machine, so my create commands are as follows:

> createdb -Omatt -Eutf8 activerecord_sinatra

Now open the "app.rb" file and edit it as so.

#app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'json'

set :database, 'postgres://matt@localhost/activerecord_sinatra'

class Course < ActiveRecord::Base
  belongs_to :teacher
end

class Teacher < ActiveRecord::Base
  has_many :courses
end

get '/' do
  "Hello World!"
end

get '/courses' do
  Course.all.to_json
end

get '/course/:id' do
  Course.find(:first, params[:id]).to_json
end

get '/teachers' do
  Teacher.all.to_json
end

get '/teacher/:id' do
  Teacher.find(:first, params[:id]).to_json
end

#show which courses the teacher teaches
get '/teacher/:id/courses' do
  Teacher.find(params[:id]).courses.to_json
end

ActiveRecord migrations are powerful, because they create a schema-independent way to control databases. Rake requires a rakefile to be present, so we will create that.

> touch Rakefil

#Rakefile
require './app'
require 'sinatra/activerecord/rake'

Now you are able to create and manage migrations, much like you can with Ruby on Rails.

> rake db:create_migration NAME=create_courses_and_teachers

This will create a directory tree in your folder of the pattern "/db/migrate/{migration}". Open the file and modify it as follows.

class CreateCoursesAndTeachers < ActiveRecord::Migration
  def self.up
    create_table :courses do |t|
      t.string :title
      t.integer :teacher_id
    end
    create_table :teachers do |t|
      t.string :name
    end
  end

  def self.down
    drop_table :courses
    drop_table :teachers
  end
end

Run the migration.

> rake db:migrate

For ease of use, you can add the following to the "app.rb" file to autogenerate some data.

#put below everything in app.rb, only run once!
get '/make' do
  teacher1 = Teacher.create(:name => "Mrs. Crabtree")
  teacher2 = Teacher.create(:name => "Alberto Yeinstein")
  Course.create(:title => "Algebra 2", :teacher_id => teacher1.id)
  Course.create(:title => "Basic Geometry", :teacher_id => teacher1.id)
  Course.create(:title => "Applied Physics", :teacher_id => teacher2.id)
  Course.create(:title => "General Relativity", :teacher_id => teacher2.id)
  Course.create(:title => "Complex Geometrical Spaces", :teacher_id => teacher2.id)
end

Now, you can run the program and data should appear at each one of the locations ("API endpoints") that you created.

> ruby app.rby

activerecordsinatra's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.