6. Associations
Parent ClassParent ClassParent Class Child Class
1 : 1
has_onehas_onehas_one
belongs_to1 : 1
has_one, :throughhas_one, :throughhas_one, :through
belongs_to
1 : n has_manyhas_manyhas_many belongs_to
n : m
A
has_manyhas_many
belongs_ton : m
A
has_many B, :throughhas_many B, :through
belongs_ton : m
B
has_manyhas_many belongs_ton : m
B
has_many A, :throughhas_many A, :through
belongs_ton : m
A / BA / B has_and_belongs_to_manyhas_and_belongs_to_many
C
8. has_many :through vs
has_and_belongs_to_many
๏ has_many
- validations, callbacks, or extra attributes
๏ has_and_belongs_to_many
- no need for additional functionalities
- should create a “join” table in the DB.