In this day we talked about a very cool design pattern called ActiveRecord.
We also talked about Rails model associations, validations and callbacks.
We finally talked about the rails query interface.
4. Active record and
MVC
What’s ActiveRecord ?
What to expect from ActiveRecord Model.
associations.
inheritance.
validations.
OO DB operations.
Convention over configuration.
5. Active Record
An object that wraps a
row in a database table,
encapsulates the database
access and adds domain
logic on that data.
6. What to expect from ActiveRecord
Model?
associations.
Relations to different models to simulate one2many, many2one self
joins, etc..
inheritance.
also is considered as relation however it’s parent-child relationship
between models.
validations.
validation logic
OO DB operations.
Business logic example:
ahmed = User.last
ahmed.joined_course(“math”)
16. Call Backs
Before, after and
around
->Save
Save<-
->Save<-
When
Create, update,
destroy
Save, validate
Executionorder
Destroy
Update
Create
17. Query interface
find(id or array(id))
take, first, last
find_by(key: value)*
where
? for sql injection
hash conditions
Ordering
Selecting
Group and Having
includes*
limit and offset
*find_each will be used with huge amount of records
*includes will eager load all associated models for less queries.