ActiveRecord Associations(1) - RORLab Season 3-7

283 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
283
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

ActiveRecord Associations(1) - RORLab Season 3-7

  1. 1. Active RecordAssociations (1)Ror lab. season 3- the 7th round -May 24, 2013ChangHoon Jeong(@seapy)
  2. 2. No AssociationsTo add an orderTo delete a customer
  3. 3. The Reason whywe should useActive RecordAssociations• Automation of connecting btw models• Automatic assignment of foreign key
  4. 4. Using Active RecordAssociationscustomer ordercustomer_idid ididparent obj. child obj.Active RecordAssocationPrimary & Foreign Keys
  5. 5. With AssociationsTo add an orderTo delete a customer
  6. 6. AssociationsParent ClassParent ClassParent Class Child Class1 : 1has_onehas_onehas_onebelongs_to1 : 1has_one, :throughhas_one, :throughhas_one, :throughbelongs_to1 : n has_manyhas_manyhas_many belongs_ton : mAhas_manyhas_manybelongs_ton : mAhas_many B, :throughhas_many B, :throughbelongs_ton : mBhas_manyhas_many belongs_ton : mBhas_many A, :throughhas_many A, :throughbelongs_ton : mA / BA / B has_and_belongs_to_manyhas_and_belongs_to_manyC
  7. 7. belongs_to vshas_one• “belongs_to” model has a foreign key• dependent upon the actual meaning of data
  8. 8. 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.
  9. 9. Join TableHABTM• Default join table btw Customer & Ordermodels: custormers_orders• Otherwise,:join_table => “join_table_name”
  10. 10. Join TableHABTMno need for join model
  11. 11. 감사합니다.
  12. 12.  

×