Git Product home page Git Product logo

pt-040521-intro-to-ar's Introduction

ActiveRecord

GEMFILE

Generating GEMFILE and adding gems

bundle init
bundle add [gem name]

Gems - General Development

  • require_all (allows us to require all files in a folder)
  • pry

Gems - ActiveRecord, Database, and Rakefile

  • activerecord
  • sinatra-activerecord (rake tasks)
  • sqlite3 (database... if using heroku you'll use pg - postgres)

Gems - Rack API

  • rack (for http requests)
  • rack-cors (for enabling cross-origin resource sharing)
  • shotgun (alternative to using rackup command so you don't have to restart server every time you make a change)

Setting up Environment File

mkdir config
touch config/environment.rb
touch config/database.yml

environment.rb

require "bundler/setup"
Bundler.require

require "json"
require "rack/cors"
require_all "app"

database.yml

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

Rakefile

touch Rakefile

Rakefile

require_relative 'config/environment'
require 'sinatra/activerecord/rake'

desc 'start console'
task :console do
    ActiveRecord::Base.logger = Logger.new(STDOUT)
    Pry.start
end

ActiveRecord Models

  • for every table, there should be a cooresponding model
mkdir app
mkdir app/models
  • lets say we have a table called notes...
touch app/models/note.rb

note.rb

class Note < ActiveRecord::Base
end

Setting up Rack API - config.ru

touch config.ru

config.ru

require_relative "./config/environment.rb"

# enables cross-origin resource sharing
# this allows any site (note origins '*') to make any request to our API

use Rack::Cors do
  allow do
    origins '*' # can also put like origins 'http://localhost:3001' and only allow that domain to send a request
    resource '/*', headers: :any, methods: [:get, :post, :patch, :put, :delete]
  end
end

# We'll create the Application class

run Application.new

Setting up Routes

touch app/application.rb

application.rb

class Application
  def call(env)
    response = Rack::Response.new
    request = Rack::Request.new(env)

    # handle all routes and send appropriate responses
  end
end

pt-040521-intro-to-ar's People

Contributors

lberge17 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.