50
Active Record Associations - 1 The 7th Round of ROR Lab. February 18th, 2012 Hyoseong Choi ROR Lab.

ActiveRecord Associations (1), Season 1

  • Upload
    rorlab

  • View
    4.912

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ActiveRecord Associations (1), Season 1

Active Record Associations - 1

The 7th Round of ROR Lab.

February 18th, 2012

Hyoseong ChoiROR Lab.

Page 2: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

Page 3: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

Page 4: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

Page 5: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

To delete orders

Page 6: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

To delete orders

Page 7: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

• Automation of connecting btw models

• Automatic assignment of foreign key

Page 8: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

customer order

id idid

Primary & Foreign Keys

Page 9: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

customer order

customer_id

id id

parent obj. child obj.

Primary & Foreign Keys

Page 10: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

customer order

customer_id

id id

parent obj. child obj.

Active Record Assocation

Primary & Foreign Keys

Page 11: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

Page 12: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

Page 13: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

Page 14: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

To delete orders

Page 15: ActiveRecord Associations (1), Season 1

ROR Lab.

For what?

To add an order

To delete orders

Page 16: ActiveRecord Associations (1), Season 1

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

Page 17: ActiveRecord Associations (1), Season 1

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

Page 18: ActiveRecord Associations (1), Season 1

ROR Lab.

belongs_to vshas_one

• “belongs_to” model has a foreign key

• dependent upon the actual meaning of data

Page 19: ActiveRecord Associations (1), Season 1

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.

Page 20: ActiveRecord Associations (1), Season 1

ROR Lab.

Join TableHABTM

• Default join table btw Customer & Order models : custormers_orders

• Otherwise, :join_table => “join_table_name”

Page 21: ActiveRecord Associations (1), Season 1

ROR Lab.

Join TableHABTM

Page 22: ActiveRecord Associations (1), Season 1

ROR Lab.

Join TableHABTM

no need for join model

Page 23: ActiveRecord Associations (1), Season 1

ROR Lab.

Product

id

Employee

id

Polymorphic

Page 24: ActiveRecord Associations (1), Season 1

ROR Lab.

Product

id

Employee

id

PolymorphicPicture

Page 25: ActiveRecord Associations (1), Season 1

ROR Lab.

Product

id

Employee

id

PolymorphicPicture

employee_id

Page 26: ActiveRecord Associations (1), Season 1

ROR Lab.

Product

id

Employee

id

Picture

PolymorphicPicture

employee_id

Page 27: ActiveRecord Associations (1), Season 1

ROR Lab.

Product

id

Employee

id

Picture

PolymorphicPicture

employee_id

product_id

Page 28: ActiveRecord Associations (1), Season 1

ROR Lab.

Product

id

Employee

id

Picture

PolymorphicPicture

employee_id

product_idx

Page 29: ActiveRecord Associations (1), Season 1

ROR Lab.

PolymorphicPicture

imageable_id

Picture

imageable_id

Imageable

Imageable

id

id

Page 30: ActiveRecord Associations (1), Season 1

ROR Lab.

PolymorphicPicture

imageable_id

Picture

imageable_id

Imageable

Imageable

Employee

Product

id

id

id

id

Page 31: ActiveRecord Associations (1), Season 1

ROR Lab.

PolymorphicModel Class

Page 32: ActiveRecord Associations (1), Season 1

ROR Lab.

PolymorphicMigration

Page 33: ActiveRecord Associations (1), Season 1

ROR Lab.

Self Joins

• ex. : following or follower

Page 34: ActiveRecord Associations (1), Season 1

ROR Lab.

Screencastfor Self Join

Please click the above link

Page 35: ActiveRecord Associations (1), Season 1

ROR Lab.

:include

• To eager-load the second-order association

Page 36: ActiveRecord Associations (1), Season 1

ROR Lab.

:include

• To eager-load the second-order association

@line_item.order.customer

Page 37: ActiveRecord Associations (1), Season 1

ROR Lab.

:include

• To eager-load the second-order association

@line_item.order.customer

Page 38: ActiveRecord Associations (1), Season 1

ROR Lab.

:counter_cache

SELECT COUNT(*) from ...

Page 39: ActiveRecord Associations (1), Season 1

ROR Lab.

:counter_cache

@customer.orders.sizeSELECT COUNT(*) from ...

Page 40: ActiveRecord Associations (1), Season 1

ROR Lab.

:counter_cache

@customer.orders.sizeSELECT COUNT(*) from ...

Page 41: ActiveRecord Associations (1), Season 1

ROR Lab.

:counter_cache

@customer.orders.sizeSELECT COUNT(*) from ...

Page 42: ActiveRecord Associations (1), Season 1

ROR Lab.

:counter_cache• But, you should add “orders_count”

column to Customer model

• You can override the default column name.

• ReadOnly !!!

Page 43: ActiveRecord Associations (1), Season 1

ROR Lab.

:counter_cache• But, you should add “orders_count”

column to Customer model

• You can override the default column name.

• ReadOnly !!!

Page 45: ActiveRecord Associations (1), Season 1

ROR Lab.

:touch

• Automatically set :updated_at or :update_on to current timestamp whenever child objects are saved or updated

Page 46: ActiveRecord Associations (1), Season 1

ROR Lab.

:touch

Page 47: ActiveRecord Associations (1), Season 1

ROR Lab.

:touch

Page 48: ActiveRecord Associations (1), Season 1

ROR Lab.

:touch

Or using a custom attribute

Page 49: ActiveRecord Associations (1), Season 1

ROR Lab.

:touch

Or using a custom attribute

Page 50: ActiveRecord Associations (1), Season 1

ROR Lab.

감사합니다.����������� ������������������