ActiveRecord Associations (1), Season 1

4,727 views
4,580 views

Published on

제7차 ROR Lab. 강의록

Published in: Education, Technology, Business
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
4,727
On SlideShare
0
From Embeds
0
Number of Embeds
2,191
Actions
Shares
0
Downloads
40
Comments
1
Likes
4
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
  • ActiveRecord Associations (1), Season 1

    1. 1. The 7th Round of ROR Lab.Active RecordAssociations - 1 February 18th, 2012 Hyoseong Choi ROR Lab.
    2. 2. For what? ROR Lab.
    3. 3. For what?To add an order ROR Lab.
    4. 4. For what?To add an order ROR Lab.
    5. 5. For what?To add an orderTo delete orders ROR Lab.
    6. 6. For what?To add an orderTo delete orders ROR Lab.
    7. 7. For what?• Automation of connecting btw models• Automatic assignment of foreign key ROR Lab.
    8. 8. For what? Primary & Foreign Keyscustomer order id id ROR Lab.
    9. 9. For what? Primary & Foreign Keyscustomer order id id customer_idparent obj. child obj. ROR Lab.
    10. 10. For what? Primary & Foreign Keyscustomer order id id Active Record customer_id Assocationparent obj. child obj. ROR Lab.
    11. 11. For what? ROR Lab.
    12. 12. For what?To add an order ROR Lab.
    13. 13. For what?To add an order ROR Lab.
    14. 14. For what?To add an orderTo delete orders ROR Lab.
    15. 15. For what?To add an orderTo delete orders ROR Lab.
    16. 16. Associations Parent Class Child Class has_one1 :1 belongs_to has_one, :through1 :n has_many belongs_to has_many A has_many B, :through belongs_ton :m has_many B has_many A, :through has_and_belongs_to_many ROR Lab.
    17. 17. 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 ROR Lab.
    18. 18. belongs_to vs has_one• “belongs_to” model has a foreign key• dependent upon the actual meaning of data ROR Lab.
    19. 19. 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. ROR Lab.
    20. 20. Join Table HABTM• Default join table btw Customer & Order models : custormers_orders• Otherwise, :join_table => “join_table_name” ROR Lab.
    21. 21. Join Table HABTM ROR Lab.
    22. 22. Join Table HABTM no need for join model ROR Lab.
    23. 23. Polymorphic Employee id Product id ROR Lab.
    24. 24. Polymorphic Employee Picture id Product id ROR Lab.
    25. 25. Polymorphic Employee Picture id employee_id Product id ROR Lab.
    26. 26. Polymorphic Employee Picture id employee_id Product Picture id ROR Lab.
    27. 27. Polymorphic Employee Picture id employee_id Product Picture id product_id ROR Lab.
    28. 28. Polymorphic Employee Picture id employee_id x Product Picture id product_id ROR Lab.
    29. 29. Polymorphic Imageable Picture id imageable_id Imageable Picture id imageable_id ROR Lab.
    30. 30. Polymorphic Imageable Picture Employee id id imageable_id Imageable Picture Product id id imageable_id ROR Lab.
    31. 31. Polymorphic Model Class ROR Lab.
    32. 32. Polymorphic Migration ROR Lab.
    33. 33. Self Joins• ex. : following or follower ROR Lab.
    34. 34. Screencastfor Self JoinPlease click the above link ROR Lab.
    35. 35. :include• To eager-load the second-order association ROR Lab.
    36. 36. :include• To eager-load the second-order association @line_item.order.customer ROR Lab.
    37. 37. :include• To eager-load the second-order association @line_item.order.customer ROR Lab.
    38. 38. :counter_cacheNT(*) from ... ROR Lab.
    39. 39. :counter_cache @customer.orders.sizeNT(*) from ... ROR Lab.
    40. 40. :counter_cache@customer.orders.size SELECT COUNT(*) from ... ROR Lab.
    41. 41. :counter_cache@customer.orders.size SELECT COUNT(*) from ... ROR Lab.
    42. 42. :counter_cache• But, you should add “orders_count” column to Customer model• You can override the default column name.• ReadOnly !!! ROR Lab.
    43. 43. :counter_cache• But, you should add “orders_count” column to Customer model• You can override the default column name.• ReadOnly !!! ROR Lab.
    44. 44. Screencast for Belongs_to:counter_cache Please click the above link ROR Lab.
    45. 45. :touch• Automatically set :updated_at or :update_on to current timestamp whenever child objects are saved or updated ROR Lab.
    46. 46. :touch ROR Lab.
    47. 47. :touch ROR Lab.
    48. 48. :touchOr using a custom attribute ROR Lab.
    49. 49. :touchOr using a custom attribute ROR Lab.
    50. 50. 감사합니다. ROR Lab.

    ×