Skip to content

Instantly share code, notes, and snippets.

@amysimmons
Last active December 26, 2017 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amysimmons/78249960884a297c2714 to your computer and use it in GitHub Desktop.
Save amysimmons/78249960884a297c2714 to your computer and use it in GitHub Desktop.

#WDI Week 3 Notes

##Monday

Australia Day Public Holiday

##Tuesday

###CRUD

  • Create a record

  • Read a record

  • Update a record

  • Delete a record

###SQL

Stands for structured query language - pronounced S-Q-L rather than SEQUEL!

Ruby has embraced the Active Record Pattern, which means Ruby translates your code into SQL.

It's also possible to use the Active Record Pattern in other languages, such as Python.

##Wednesday

###Notes from Rails Guides' Active Record Basics

http://guides.rubyonrails.org/active_record_basics.html

Active Record:

Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

Object relational mapping:

Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.

Convention over Configuration in Active Record:

If you follow the conventions adopted by Rails, you'll need to write very little configuration (in some case no configuration at all) when creating Active Record models.

The idea is that if you configure your applications in the very same way most of the time then this should be the default way. Thus, explicit configuration would be needed only in those cases where you can't follow the standard convention.

Naming conventions:

Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books.

Model Class - Singular with the first letter of each word capitalized (e.g., BookClub).

Model Class > CamelCase

Database Table - Plural with underscores separating words (e.g., book_clubs).

Database Table > line_items

Creating Active Record Models:

class Product < ActiveRecord::Base
end

This will create a Product model, mapped to a products table at the database.

CRUD: Reading and Writing Data

Create method: Given a model User with attributes of name and occupation, the create method call will create and save a new record into the database:

user = User.create(name: "David", occupation: "Code Artist")

New method: Using the new method, an object can be instantiated without being saved:

user = User.new
user.name = "David"
user.occupation = "Code Artist"

user.save would commit the record to the database.

Accessing data:

Examples:

# return a collection with all users
users = User.all

# return the first user
user = User.first

# return the first user named David
david = User.find_by(name: 'David')

# find all users named David who are Code Artists and sort by created_at in reverse chronological order
users = User.where(name: 'David', occupation: 'Code Artist').order('created_at DESC')

More here: http://guides.rubyonrails.org/active_record_querying.html

Updating data:

Some examples:

user = User.find_by(name: 'David')
user.name = 'Dave'
user.save
#shorthand version of above

user = User.find_by(name: 'David')
user.update(name: 'Dave')

Deleting data:

user = User.find_by(name: 'David')
user.destroy

Validations:

class User < ActiveRecord::Base
  validates :name, presence: true
end
 
user = User.new
user.save  # => false
user.save! # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank

Callbacks:

Enable you to add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on.

###Datatypes in SQLite Version 3

http://www.sqlite.org/datatype3.html

  • NULL: The value is a NULL value.

  • INTEGER: The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

  • REAL: The value is a floating point value, stored as an 8-byte IEEE floating point number.

  • TEXT: The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

  • BLOB: The value is a blob of data, stored exactly as it was input.

Note: SQLite does not have a separate Boolean storage class. Boolean values are stored as integers 0 (false) and 1 (true).

##Thursday

###Ruby style guide!

https://github.com/styleguide/ruby

###Morning project:

https://gist.github.com/epoch/d9119d53b877f60ec955

I did this with a gem, must come back and re-do without!

require 'numbers_and_words'

class Say 

    def initialize(num)
        @num = num
    end

    def in_english
        if @num < 0 || @num > 99
            puts "Number out of range."
        else
            puts @num.to_words
        end
    end

end

Say.new(100).in_english
Say.new(99).in_english

###CRUD in SQL, Active Record and HTTTP

Create:

-SQL: INSERT

-AR: .save, .create

-HTTP: POST

Read:

-SQL: SELECT

-AR: .find, .find_by, .all, .where, .select

-HTTP: GET

Update:

-SQL: UPDATE

-AR: .save, .update, .update-attributes

-HTTP: POST, (PATCH), (PUT)

Delete:

-SQL: DELETE

-AR: .destroy

-HTTP: GET/POST (technically more of a post), (DELETE)

##Friday

###Gems

###Deployment

Creating a new Heroku app:

touch Gemfile

source 'https://rubygems.org'
gem 'sinatra'
gem 'httparty'
gem 'json'

bundle

touch config.ru

require './main.rb'
run Sinatra::Application

rackup

git init

git add .

git commit -m

heroku create carnival-games

git remote -v

git push heroku master

Updating an app:

git status

git remote -v

git add .

git commit -m

git push heroku master

Deployed the following two homework tasks:

http://carnival-games.herokuapp.com/

http://movie-poster-search.herokuapp.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment