SlideShare a Scribd company logo
Ruby on rails
course
Day 3
M for Model
Agenda
ActiveRecord Basics
Associations
Validations
Callbacks
Query Interface
Active record and
MVC
What’s ActiveRecord ?
What to expect from ActiveRecord Model.
associations.
inheritance.
validations.
OO DB operations.
Convention over configuration.
Active Record
An object that wraps a
row in a database table,
encapsulates the database
access and adds domain
logic on that data.
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”)
Convention over
configuration
Model class name
and table name
Foreign and primary
key convention:
FK:
<table_name>_id
PM:

id
Rails g model
<model_name>
[field:type, ]
Creates model file
Creates model test
Created migration
file.
Associations
belongs_to
has_one
how is that different
than belongs_to ?
through
has_many
through
has_and_belongs_to_many
belongs_to
One2One
connection.
single model
belongs_to :depart
ment
has_one
has_one is One2One
connection.
single model
has_one <model>
No FK required in
model, it’s pointed in
the other model.
through is the
intermediate junk table.
has_many
pointing to me.
has_many through :model
VS. has_and_belongs_to_many
Self Join
class_name
foreign_key
class Employee < ActiveRecord::Base

has_many :subordinates, class_name:
"Employee",foreign_key: "manager_id"
Validations
Built-in

validates :name, presence: true, length:
{minimum: 3}
Class Validator, EachValidator
Template design pattern
Methods
on:
Call Backs
Before, after and
around

->Save

Save<-

->Save<-
When
Create, update,
destroy
Save, validate

Executionorder
Destroy
Update
Create
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.
Thank you!

More Related Content

PDF
Web Programming - 5 Passing and Request Data
DOCX
Web 10mca556 syllabs
PPTX
Building nTier Applications with Entity Framework Services
PPT
Entity framework1
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
PPTX
Using schemas in parsing xml part 1
Web Programming - 5 Passing and Request Data
Web 10mca556 syllabs
Building nTier Applications with Entity Framework Services
Entity framework1
Building nTier Applications with Entity Framework Services (Part 1)
Using schemas in parsing xml part 1

What's hot (11)

PDF
Asp net interview_questions
PDF
Object oriented databases
PDF
SURVEY ON MONGODB: AN OPEN-SOURCE DOCUMENT DATABASE
DOCX
Codd rules
PDF
Storage dei dati con MongoDB
PPT
Introduction to Data Modeling
PPT
Data Binding In Depth
PPTX
Drupal as a data server
PDF
JSON Application
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
PDF
Chapter17 oop
Asp net interview_questions
Object oriented databases
SURVEY ON MONGODB: AN OPEN-SOURCE DOCUMENT DATABASE
Codd rules
Storage dei dati con MongoDB
Introduction to Data Modeling
Data Binding In Depth
Drupal as a data server
JSON Application
Building nTier Applications with Entity Framework Services (Part 1)
Chapter17 oop
Ad

Similar to Rails course day 3 (20)

PDF
Principles of MVC for Rails Developers
PPT
Ruby on Rails: Building Web Applications Is Fun Again!
DOCX
Framework 4
PPT
Ruby On Rails
PPT
MVC Demystified: Essence of Ruby on Rails
PDF
Ruby on rails RAD
PPSX
Abhishek_DBMS-ch1_Database_management.ppsx
PDF
IRJET- Lightweight MVC Framework in PHP
PDF
Free Hibernate Tutorial | VirtualNuggets
PDF
Stucorner dot-net-training-syllabus
PPTX
MIT302 Lesson 2_Advanced Database Systems.pptx
PPTX
Introduction of C# & MVC
PPTX
Ef code first
PPTX
Ruby on rails for beginers
PDF
Datastage coursecontent
PDF
Data stage
PDF
Data stage docx
PDF
Building social and RESTful frameworks
PDF
Vital AI MetaQL: Queries Across NoSQL, SQL, Sparql, and Spark
PPT
Principles of MVC for Rails Developers
Ruby on Rails: Building Web Applications Is Fun Again!
Framework 4
Ruby On Rails
MVC Demystified: Essence of Ruby on Rails
Ruby on rails RAD
Abhishek_DBMS-ch1_Database_management.ppsx
IRJET- Lightweight MVC Framework in PHP
Free Hibernate Tutorial | VirtualNuggets
Stucorner dot-net-training-syllabus
MIT302 Lesson 2_Advanced Database Systems.pptx
Introduction of C# & MVC
Ef code first
Ruby on rails for beginers
Datastage coursecontent
Data stage
Data stage docx
Building social and RESTful frameworks
Vital AI MetaQL: Queries Across NoSQL, SQL, Sparql, and Spark
Ad

More from Al Sayed Gamal (20)

PDF
9 patterns of microservices
PPTX
Elixir introduction
PDF
Racist syndrome (composition over inheritance)
PDF
Ionic Hybrid Mobile Application
PDF
Rails course day 8
PDF
Rails course day 7
PDF
Rails course day 6
PDF
Rails course day 5
PDF
Rails course day 4
PDF
Rails course day 2
PDF
PDF
OpenStack Murano Application Catalog
PDF
Failfast
PDF
Git workshop
PDF
Python Novice to Ninja
PDF
Web development Hackathon
PDF
Python novice to ninja
PDF
Python 45 minutes hangout #3
PDF
Python 45 minutes Hangouts #4
PDF
Python45 2
9 patterns of microservices
Elixir introduction
Racist syndrome (composition over inheritance)
Ionic Hybrid Mobile Application
Rails course day 8
Rails course day 7
Rails course day 6
Rails course day 5
Rails course day 4
Rails course day 2
OpenStack Murano Application Catalog
Failfast
Git workshop
Python Novice to Ninja
Web development Hackathon
Python novice to ninja
Python 45 minutes hangout #3
Python 45 minutes Hangouts #4
Python45 2

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
history of c programming in notes for students .pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
PTS Company Brochure 2025 (1).pdf.......
ai tools demonstartion for schools and inter college
Designing Intelligence for the Shop Floor.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
assetexplorer- product-overview - presentation
Navsoft: AI-Powered Business Solutions & Custom Software Development
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Nekopoi APK 2025 free lastest update
Internet Downloader Manager (IDM) Crack 6.42 Build 41
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo Companies in India – Driving Business Transformation.pdf
Operating system designcfffgfgggggggvggggggggg
Digital Systems & Binary Numbers (comprehensive )
history of c programming in notes for students .pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
top salesforce developer skills in 2025.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PTS Company Brochure 2025 (1).pdf.......

Rails course day 3