Rails Decal
Lecture 3 - Models, DB, CRUD
Today:
- What models are, and why we need them
- Brief database introduction
- How to interact with models
- How to interact with model controllers
- Live demo. Make users!
- Lab time!
MVC
First, an intro to classes
- To those not familiar with Object Oriented Programming (61A/B), a class is created in programming like variables are but they have super special powers.
# Variables
year = 1993
shape = "square"
# Class
class User
def initialize(age, email)
@age = age
@email = email
end
def email
@email
end
# shortcut
attr_accessor :age
def birthday()
@age += 1
end
end
>> Wonjun = User.new(99, "old@old.com")
>> Wonjun.age
=> 99
>> Wonjun.birthday()
>> Wonjun.age
=> 100
Models in Rails!
- Models are built in class frameworks
- Have methods that you don't need to define
- Interact with the database automatically for you***
- They do a bunch of other cool things like relationships and validations that will be covered later
- They can do all of that because of this:
class <classname> < ActiveRecord::Base
Rails is magic
Why models are important
- Models are the core of Rails apps
- Represents all the information you need
- This allows controllers to work with the model
- Represents all the information you need
Future Lectures:
- Validations
- Relationships
- Add more abilities (methods)
- and more!
Quick Info about Databases
- They hold all of your data for you
- Don't need to know much about Databases
- **Migrations are what keep models and the database in sync. And unfortunately, we must run those ourselves
So what can I do with a model?
-
Question:
- For an app Twitter, with many different Users, what should the Twitter app be able to do to each of these users?
-
CRUD
- Create
- Read (Show)
- Update (Edit)
- Destroy
- And we can do this using CONTROLLERS!!!!!
Controllers and CRUD
Today we'll focus on
- Create
- Read
- Update
CRUD - Create
- Usually done through forms
- A person on a Rails app will pass in values like
- Name: Howard
- Email: swag@swag.com
- Age: 9001
- And the controller will create a Model Instance
- And add that to the database
CRUD - Read (Show/Index)
- In the view the controller can specify which of the model objects to show
- Should it just show Wonjun?
- Should it show Wonjun, Sam, Howard?
- Should it only show old people?
- Etc.
- The controller will select the correct ones accordingly and show them
CRUD - Update (Update/Edit)
- Also usually done through forms
- A person on a Rails app will maybe want to change the name of themselves
- Previous name: Howard
- New name: JSONDerulo
- And the controller will update that model instance (database row)
Live Demo
- Create user model/controller/views from scratch
- Run migrations to have these work
- Utilize Rails console
https://github.com/rails-decal/week4-lab
Warm ups:
- Add a new user to the database
- Add yourself!
- Edit a user in the database.
- Rename yourself!
Work Time:
- Add a new field to the user called phone
- Show only old people on the index
- Delete the age field for the user (and the db column)
-
- Hint: Look at different functions you can call on a model
Week 3 - Models, DB, CRUD
By Rails Decal
Week 3 - Models, DB, CRUD
- 2,194