Lecture 4 - Models & Migrations
Charles X.
Text
Database for sample_app
class <classname> < ActiveRecord::Base
Future Lectures:
rails generate model model_name column_name:column_type
rails generate model User name:string age:integer
rails generate migration migration_name
rake db:migrate
Adding columns
Assume we have table called User
add_column :users, :nickname, :string
Removing columns
remove_column :users, :nickname
Renaming columns
rename_column :users, :name, :nickname
Model.find id
Model.find_by attribute: something
Model.where attribute: something
Model.first/last
Model.all
Today we'll focus on
Making users locally - Loads our app
rails console OR rails c
Making a new User
user = User.new
user.save!
user = User.create name: "hello", age: 5
`
user.name = "Charles"
OR
Live Demo
Thats cool and all but..
Fork: https://github.com/rails-decal/sp15-hw3