Rails: Models & ActiveRecord
Building an app
Our class Rails app will be "Lekker Plekke", which means "Cool Places" in Afrikaans.
We'll use it to keep track of all of the cool places that we find in Cape Town and South Africa.
ActiveRecord
ActiveRecord is the Rails library for models.
What's a model?
Rails object that has methods for managing data
WHAT ARE MODELS RESPONSIBLE FOR?
Managing a single database table
Providing Ruby methods to access data
Application Logic (aka Business logic)
What's a database?
Permanently stores data
What's a table?

Database operations
Doing anything with data is always one of these operations.
Animal.create name: "Giraffe", description: "Has a realllly long neck."
giraffe = Animal.find_by name: "Giraffe"
giraffe.update name: "Baby Giraffe"
giraffe.destroy
Rails Naming
Models are always singular.
(User, Movie, Animal)
Tables are always plural.
(users, movies, animals)
Controllers are always plural.
(UsersController, MoviesController, AnimalsController)
Demonstrating Rails Models
Building Lekker Plekke
Do this outside of your ixperience
folder.
This will be it's own repository!
rails new lekker_plekke
cd lekker_plekke
git init
git add .
git commit -m"Initial Rails commit"
Databases Recap
- Databases are made of records, with data-typed columns
- Used to permanently store data
- All data operations are (C)reate, (R)ead, (U)pdate, or (D)elete.
- Databases use the SQL language
Models Recap
- Models correspond with one data record
- Models: singular, tables: plural
- class ModelName < ActiveRecord::Base
- Query methods:
all
, where
, find
, find_by
, first
- Migrations are used to change the database schema
- Never manually edit
schema.rb
!
- Seeds are for putting default data in our database