ActiveRecord Association (1), Season 2

1,195 views

Published on

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

Published in: Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,195
On SlideShare
0
From Embeds
0
Number of Embeds
357
Actions
Shares
0
Downloads
21
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. Ror lab. season 2 - the 8th round - Active RecordAssociations (1) October 20th, 2012 Hyoseong Choi
    2. 2. No Associations
    3. 3. No AssociationsTo add an order
    4. 4. No AssociationsTo add an orderTo delete a customer
    5. 5. The Reason why we should use Active Record Associations• Automation of connecting btw models• Automatic assignment of foreign key
    6. 6. Using Active Record Associations Primary & Foreign Keys customer order id id
    7. 7. Using Active Record Associations Primary & Foreign Keys customer order id id customer_id parent obj. child obj.
    8. 8. Using Active Record Associations Primary & Foreign Keys customer order id id Active Record customer_id Assocation parent obj. child obj.
    9. 9. With Associations
    10. 10. With Associations To add an order
    11. 11. With Associations To add an order To delete a customer
    12. 12. 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
    13. 13. 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
    14. 14. belongs_to vs has_one• “belongs_to” model has a foreign key• dependent upon the actual meaning of data
    15. 15. 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.
    16. 16. Join Table HABTM• Default join table btw Customer & Order models : custormers_orders• Otherwise, :join_table => “join_table_name”
    17. 17. Join Table HABTM
    18. 18. Join Table HABTM no need for join model
    19. 19. Polymorphic Employee id Product id
    20. 20. Polymorphic Employee Picture id Product id
    21. 21. Polymorphic Employee Picture id employee_id Product id
    22. 22. Polymorphic Employee Picture id employee_id Product Picture id
    23. 23. Polymorphic Employee Picture id employee_id Product Picture id product_id
    24. 24. Polymorphic Employee Picture id employee_id x Product Picture id product_id
    25. 25. Polymorphic What about this “Picture” model ?Employee Product id id
    26. 26. Polymorphic What about this “Picture” model ?Employee Picture Product id employee_id id product_id
    27. 27. Polymorphic What about this “Picture” model ?Employee Picture Product Picture id employee_id id product_id product_id employee_id
    28. 28. Polymorphic What about this “Picture” model ?Employee Picture Product Picture id employee_id id product_id product_id employee_id
    29. 29. Polymorphic Picture Employee id Picture Product id
    30. 30. Polymorphic Picture Employee id :as => :imageable Picture Product id
    31. 31. Polymorphic Imageable Picture Employee id id Imageable Picture Product id id
    32. 32. Polymorphic Imageable Picture Employee:imageable_type id id imageable_id :imageable_id Imageable Picture Product id id imageable_id
    33. 33. Polymorphic Model Class
    34. 34. Polymorphic Migration
    35. 35. Self Joins• ex. : following or follower
    36. 36. Screencastfor Self JoinPlease click the above link
    37. 37. :include• To eager-load the second-order association
    38. 38. :include• To eager-load the second-order association @line_item.order.customer
    39. 39. :include• To eager-load the second-order association @line_item.order.customer
    40. 40. :counter_cacheNT(*) from ...
    41. 41. :counter_cache @customer.orders.sizeNT(*) from ...
    42. 42. :counter_cache@customer.orders.size SELECT COUNT(*) from ...
    43. 43. :counter_cache@customer.orders.size SELECT COUNT(*) from ...
    44. 44. :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 !!!
    45. 45. :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 !!!
    46. 46. Screencast for Belongs_to:counter_cache Please click the above link
    47. 47. :touch• Automatically set :updated_at or :update_on to current timestamp whenever child objects are saved or updated
    48. 48. :touch
    49. 49. :touch
    50. 50. :touchOr using a custom attribute
    51. 51. :touchOr using a custom attribute
    52. 52. 감사합니다.
    53. 53.  

    ×