Active Record Associations - 1
The 7th Round of ROR Lab.
February 18th, 2012
Hyoseong ChoiROR Lab.
ROR Lab.
For what?
ROR Lab.
For what?
To add an order
ROR Lab.
For what?
To add an order
ROR Lab.
For what?
To add an order
To delete orders
ROR Lab.
For what?
To add an order
To delete orders
ROR Lab.
For what?
• Automation of connecting btw models
• Automatic assignment of foreign key
ROR Lab.
For what?
customer order
id idid
Primary & Foreign Keys
ROR Lab.
For what?
customer order
customer_id
id id
parent obj. child obj.
Primary & Foreign Keys
ROR Lab.
For what?
customer order
customer_id
id id
parent obj. child obj.
Active Record Assocation
Primary & Foreign Keys
ROR Lab.
For what?
ROR Lab.
For what?
To add an order
ROR Lab.
For what?
To add an order
ROR Lab.
For what?
To add an order
To delete orders
ROR Lab.
For what?
To add an order
To delete orders
ROR Lab.
AssociationsParent ClassParent Class Child Class
1 : 1has_onehas_one
belongs_to1 : 1has_one, :throughhas_one, :through
belongs_to
1 : n has_manyhas_many belongs_to
n : m
Ahas_many
belongs_ton : m
Ahas_many B, :through
belongs_ton : m
Bhas_many
belongs_ton : m
Bhas_many A, :through
belongs_ton : m
has_and_belongs_to_manyhas_and_belongs_to_manyhas_and_belongs_to_many
ROR Lab.
Association Optionshas_one options• :as• :autosave• :class_name• :conditions• :dependent• :foreign_key• :include• :order• :primary_key• :readonly• :select• :source• :source_type• :through• :validate
has_many options• :as• :autosave• :class_name• :conditions• :counter_sql• :dependent• :extend• :finder_sql• :foreign_key• :group• :include• :limit• :offset• :order• :primary_key• :readonly• :select• :source• :source_type• :through• :uniq• :validate
HABTM options• :association_foreign_key• :autosave• :class_name• :conditions• :counter_sql• :delete_sql• :extend• :finder_sql• :foreign_key• :group• :include• :insert_sql• :join_table• :limit• :offset• :order• :readonly• :select• :uniq• :validate
belongs_to options• :autosave• :class_name• :conditions• :counter_cache• :dependent• :foreign_key• :include• :polymorphic• :readonly• :select• :touch• :validate
ROR Lab.
belongs_to vshas_one
• “belongs_to” model has a foreign key
• dependent upon the actual meaning of data
ROR Lab.
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.
ROR Lab.
Join TableHABTM
• Default join table btw Customer & Order models : custormers_orders
• Otherwise, :join_table => “join_table_name”
ROR Lab.
Join TableHABTM
ROR Lab.
Join TableHABTM
no need for join model
ROR Lab.
Product
id
Employee
id
Polymorphic
ROR Lab.
Product
id
Employee
id
PolymorphicPicture
ROR Lab.
Product
id
Employee
id
PolymorphicPicture
employee_id
ROR Lab.
Product
id
Employee
id
Picture
PolymorphicPicture
employee_id
ROR Lab.
Product
id
Employee
id
Picture
PolymorphicPicture
employee_id
product_id
ROR Lab.
Product
id
Employee
id
Picture
PolymorphicPicture
employee_id
product_idx
ROR Lab.
PolymorphicPicture
imageable_id
Picture
imageable_id
Imageable
Imageable
id
id
ROR Lab.
PolymorphicPicture
imageable_id
Picture
imageable_id
Imageable
Imageable
Employee
Product
id
id
id
id
ROR Lab.
PolymorphicModel Class
ROR Lab.
PolymorphicMigration
ROR Lab.
Self Joins
• ex. : following or follower
ROR Lab.
Screencastfor Self Join
Please click the above link
ROR Lab.
:include
• To eager-load the second-order association
ROR Lab.
:include
• To eager-load the second-order association
@line_item.order.customer
ROR Lab.
:include
• To eager-load the second-order association
@line_item.order.customer
ROR Lab.
:counter_cache
SELECT COUNT(*) from ...
ROR Lab.
:counter_cache
@customer.orders.sizeSELECT COUNT(*) from ...
ROR Lab.
:counter_cache
@customer.orders.sizeSELECT COUNT(*) from ...
ROR Lab.
:counter_cache
@customer.orders.sizeSELECT COUNT(*) from ...
ROR Lab.
:counter_cache• But, you should add “orders_count”
column to Customer model
• You can override the default column name.
• ReadOnly !!!
ROR Lab.
:counter_cache• But, you should add “orders_count”
column to Customer model
• You can override the default column name.
• ReadOnly !!!
ROR Lab.
Screencast for Belongs_to
:counter_cache
Please click the above link
ROR Lab.
:touch
• Automatically set :updated_at or :update_on to current timestamp whenever child objects are saved or updated
ROR Lab.
:touch
ROR Lab.
:touch
ROR Lab.
:touch
Or using a custom attribute
ROR Lab.
:touch
Or using a custom attribute
ROR Lab.
감사합니다.����������� ������������������