Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save mathildathompson/9497789 to your computer and use it in GitHub Desktop.

Select an option

Save mathildathompson/9497789 to your computer and use it in GitHub Desktop.
##ENVIRONEMNTS
#Development (uses sqlite)
#Production (uses postgresql)
#Test environemnt
psql to get into the database
\l shows all of the databases;
\c students (connects to the students database);
\d shows relations;
##DATATYPES: A lot of the datatypes are specific to postgres
#BIG INT 2*64
#BIG SERIAL (auto increments, good for ids if they are going to be massive numbers)
#BOX TYPE (store points together, good for plotting stuff on maps)
#VARCHAR(7) (this can only be 7 digits long, useful for username and password)
#DATE TYPE (you can say get me all the things that were before a certain date)
#INTEGER (smaller than 32 * 2)
#MONEY
#TEXT
#XML
#JSON
psql students < students.sql
SELECT COUNT(*) FROM STUDENTS;
SELECT MAX(age) FROM STUDENTS;
#id of a table is a primary key;
#A primary key in another table is a foreign key;
##LOOKING FOR STUFF
#gem list | active
#gem list | head
#gem list |tail
##ACTIVE RECORD
#Just tell me in the database what the columns are
require 'active_record' #You only have to do this in Sinatra, rails uses ActiveRecord by default;
#ActiveRecord is a module that is going to sit between out application and the postgresql database;
#Out class need to inherit from ActiveRecord::Base;
#We now have to use attr_accessible inside of the class (as opposed to attr_accessor);
#With Active Record we are getting objects out of the database;
#We are no longer using keys to access values in a hash, we are accessing attributes in the object so we use the . notation;
#Butterfly.find params[:id] #When you do a find it returns a single object and not an array
##DELETE
butterfly = Butterfly.find params[:id]
butterfly.destroy #Delete will just delete it, destroy will delete it and run anything that it needs to clean it up;
##EDIT
@butterfly = Butterfly.find(params[:id])
##UPDATE
butterfly = Butterfly.find(params[:id])
butterfly.name = params[:name]
butterfly.family = params[:family]
butterfly.photo = params[:photo]
butterfly.save
#CREATE
butterfly = Butterly.new
butterfly.name = params[:name] #This was passed through in the form
#......and so on with all of the other attribtues
butterfly.save
Butterfly.select(:family).distinct #Selects the distinct butterfly class families
#REMEMBER!!!!
class Butterfly
database table butterflies #The name of the database table will always be pluralized;
##ACTIVE RECORD is not only a rails thing, also have it in Javascriprt, Python;
#An object will not have an id until it has been saved in the database;
b.family = "Kristophen"
b.changed?
b.changes
b.save
#To see methods that are available
burger = Burger.new
burger.methods #gives all the things that ruby gives for free when you create a class;
HOMEWORK!
create table blogs (
id serial8 PRIMARY KEY,
title TEXT,
copy TEXT,
mood TEXT,
author TEXT,
phtot TEXT
) #Create the table and put in into the blogs database #Get the table and hook it up with Active Record
#Turn into a CRUD active record app for homework;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment