ActiveRecord Association (1), Season 2
Upcoming SlideShare
Loading in...5
×
 

ActiveRecord Association (1), Season 2

on

  • 944 views

This is a Keynote slide about the "ActiveRecord Association (1)"

This is a Keynote slide about the "ActiveRecord Association (1)"

Statistics

Views

Total Views
944
Views on SlideShare
588
Embed Views
356

Actions

Likes
0
Downloads
12
Comments
0

7 Embeds 356

http://rorlab.org 190
http://lab.wordstory.net 128
http://livecoding.rorlab.org 32
http://www.rorlab.org 3
http://translate.googleusercontent.com 1
http://211.60.184.138 1
http://duckduckgo.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

ActiveRecord Association (1), Season 2 ActiveRecord Association (1), Season 2 Presentation Transcript

  • Ror lab. season 2 - the 8th round - Active RecordAssociations (1) October 20th, 2012 Hyoseong Choi
  • No Associations
  • No AssociationsTo add an order
  • No AssociationsTo add an orderTo delete a customer
  • The Reason why we should use Active Record Associations• Automation of connecting btw models• Automatic assignment of foreign key
  • Using Active Record Associations Primary & Foreign Keys customer order id id
  • Using Active Record Associations Primary & Foreign Keys customer order id id customer_id parent obj. child obj.
  • Using Active Record Associations Primary & Foreign Keys customer order id id Active Record customer_id Assocation parent obj. child obj.
  • With Associations
  • With Associations To add an order
  • With Associations To add an order To delete a customer
  • Associations Parent Class Child Class has_one1 :1 belongs_to has_one, :through1 :n has_many belongs_to has_many A C has_many B, :throughn :m has_many belongs_to B has_many A, :through A/B has_and_belongs_to_many
  • Association Optionsbelongs_to options has_one options has_many options HABTM options• :autosave • :as • :as • :association_foreign_key• :class_name • :autosave • :autosave • :autosave• :conditions • :class_name • :class_name • :class_name• :counter_cache • :conditions • :conditions • :conditions• :dependent • :dependent • :counter_sql • :counter_sql• :foreign_key • :foreign_key • :dependent • :delete_sql• :include • :include • :extend • :extend• :polymorphic • :order • :finder_sql • :finder_sql• :readonly • :primary_key • :foreign_key • :foreign_key• :select • :readonly • :group • :group• :touch • :select • :include • :include• :validate • :source • :limit • :insert_sql • :source_type • :offset • :join_table • :through • :order • :limit • :validate • :primary_key • :offset • :readonly • :order • :select • :readonly • :source • :select • :source_type • :uniq • :through • :validate • :uniq • :validate
  • belongs_to vs has_one• “belongs_to” model has a foreign key• dependent upon the actual meaning of data
  • has_many :through vshas_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.
  • Join Table HABTM• Default join table btw Customer & Order models : custormers_orders• Otherwise, :join_table => “join_table_name”
  • Join Table HABTM
  • Join Table HABTM no need for join model
  • Polymorphic Employee id Product id
  • Polymorphic Employee Picture id Product id
  • Polymorphic Employee Picture id employee_id Product id
  • Polymorphic Employee Picture id employee_id Product Picture id
  • Polymorphic Employee Picture id employee_id Product Picture id product_id
  • Polymorphic Employee Picture id employee_id x Product Picture id product_id
  • Polymorphic What about this “Picture” model ?Employee Product id id
  • Polymorphic What about this “Picture” model ?Employee Picture Product id employee_id id product_id
  • Polymorphic What about this “Picture” model ?Employee Picture Product Picture id employee_id id product_id product_id employee_id
  • Polymorphic What about this “Picture” model ?Employee Picture Product Picture id employee_id id product_id product_id employee_id
  • Polymorphic Picture Employee id Picture Product id
  • Polymorphic Picture Employee id :as => :imageable Picture Product id
  • Polymorphic Imageable Picture Employee id id Imageable Picture Product id id
  • Polymorphic Imageable Picture Employee:imageable_type id id imageable_id :imageable_id Imageable Picture Product id id imageable_id
  • Polymorphic Model Class
  • Polymorphic Migration
  • Self Joins• ex. : following or follower
  • Screencastfor Self JoinPlease click the above link
  • :include• To eager-load the second-order association
  • :include• To eager-load the second-order association @line_item.order.customer
  • :include• To eager-load the second-order association @line_item.order.customer
  • :counter_cacheNT(*) from ...
  • :counter_cache @customer.orders.sizeNT(*) from ...
  • :counter_cache@customer.orders.size SELECT COUNT(*) from ...
  • :counter_cache@customer.orders.size SELECT COUNT(*) from ...
  • :counter_cache• But, you should add “orders_count” column to Customer model• Default column name : table name + “_count”• You can override the default column name.• ReadOnly !!!
  • :counter_cache• But, you should add “orders_count” column to Customer model• Default column name : table name + “_count”• You can override the default column name.• ReadOnly !!!
  • Screencast for Belongs_to:counter_cache Please click the above link
  • :touch• Automatically set :updated_at or :update_on to current timestamp whenever child objects are saved or updated
  • :touch
  • :touch
  • :touchOr using a custom attribute
  • :touchOr using a custom attribute
  • 감사합니다.
  •