Upgrade to Pro — share decks privately, control downloads, hide ads and more …

chou

 chou

E473e5af1263c050599921981db46068?s=128

akatsukinewgrad

January 08, 2021
Tweet

Transcript

  1. ΞΧπΩ ͪΐ͏ 3BJMTͷ/ ͋Δ͋Δ 

  2. ࣗݾ঺հɿͪΐ͏ ΞΧπΩ৽ଔ ήʔϜͷαʔόʔΤϯδχΞ ݱ໾֎ࠃਓ -FBHVFPG-FHFOETΛ΍͍ͬͯ·͢ ࠷ۙ͸$ZCFSQVOL΋΍ͬͯ·͢  ちょうの写真でした

  3. ࠓ೔࿩͢͜ͱ ήʔϜαʔόʔͰى͖͕ͪͳ/ ΫΤϦ໰୊ ʹ͍ͭͯ ࢲ͕ීஈ৺ֻ͚͍ͯΔ͜ͱΛ͓࿩͠͠·͢ 

  4. / ΫΤϦ໰୊ͱ͸ ؆୯ʹݴ͏ͱɺΫΤϦ͕ଟ͗͢Δ໰୊ جຊతͳ՝୊Ͱ͋Γɺ࣌ʹͱͯ΋ෳࡶͳ՝୊Ͱ͋Δ ࠓ೔͸ʮ3VCZPO3BJMTʯͰ͜ͷ࿩Λ͠·͢ ʢ΄͔ͷݴޠɺϑϨʔϜϫʔΫʹ͓͍ͯ΋΄΅௨༻͢Δʣ 

  5. 3VCZ PO3BJMT 3VCZͰͰ͖͍ͯΔXFCͷϑϨʔϜϫʔΫ બΜͩཧ༝ɿීஈۀ຿Ͱ࢖͍ͬͯΔ͔ΒͰ͢ʂ 

  6. "DUJWF3FDPSE σʔλϕʔεͷϨίʔυΛϞσϧԽ  %#ͷΧϥϜϞσϧͷଐੑ  0CKFDU3FMBUJPOBMNBQQJOH 03. class ApplicationRecord <

    ActiveRecord::Base end class User < ApplicationRecord end user = User.find(1) puts user.id # 1 puts user.name # Taro id name 1 Taro 2 Jiro 3 Saburo Usersテーブル M-V-C 
  7. Ϟσϧͷؔ࿈ΛදݱͰ͖Δ id name 1 Taro 2 Jiro 3 Saburo usersテーブル

    id user_id address 1 1 Tokyo 2 1 Yokohama 3 2 Saga 4 2 Chiba 5 3 Tokyo user_addressesテーブル 例えば、1対他のassociation class User < ApplicationRecord has_many :user_addresses end class UserAddress < ApplicationRecord belongs_to :user end user1 = User.find(1) user1.user_addresses # <UserAddress id: 1>, <UserAddress id: 2> user1.user_addresses.map(&:address) # ['Tokyo', ‘Yokohama'] 
  8. Կ͕ى͖Δ  ؔ࿈σʔλ͕ඞཁ  %#ͰΫΤϦΛ࣮ߦͯ͠ σʔλΛऔಘ user1 = User.find(1) user1.user_addresses

    # <UserAddress id: 1>, <UserAddress id: 2> SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 1 
  9. ϧʔϓ͢ΔͱʢҰ൪యܕతͳέʔεʣ users = User.all users.each do |user| puts user.user_addresses end

    SELECT "users".* FROM "users" SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 1 SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 2 SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 3 N 1 + ΫΤϦ͕͍ͬͺ͍ൃߦ͞ΕΔ ήʔϜαʔόʔͰ/͕࣌ʹઍ୯Ґ 
  10. ήʔϜαʔόʔʹ͓͍ͯɺσʔλϕʔε͸Ұ൪وॏͳࢿݯ "11αʔόʔ͸ɺ؆୯ʹεέʔϧௐ੔͕Ͱ͖Δ  ௨ৗɺ୆਺Λ૿΍ͤ͹ͳΜͱ͔ͳΔ %#͸ɺͦ͏͸͍͔ͳ͍  εέʔϧௐ੔͕೉͍͠ɺ෼ׂ͕͞Βʹ೉͍͠ %#ͷෛՙʹؾΛ͚ͭͳ͕Β։ൃ͢Δඞཁ͕͋Δ 

  11. / ໰୊ΛͲ͏ղܾ͢Δʁ جຊతͳղܾํ๏ɿQSFMPBE͢Δ ͋ͱͰඞཁʹͳΔෳ਺ͷϨίʔυΛࣄલʹ·ͱΊͯऔ͓ͬͯ͘͜ͱ ൒෼Ҏ্ͷέʔε͸͜ΕͰղܾͰ͖Δʂ users = User.includes(:user_addresses).all SELECT "users".*

    FROM "users" SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" IN (1,2,3) SELECT "users".* FROM "users" SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 1 SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 2 SELECT "user_addresses".* FROM "user_addresses" WHERE "user_addresses"."user_id" = 3 before after 
  12. / ൃੜ͸TFMFDUจ͚ͩͰ͸ͳ͍ JOTFSU VQEBUF EFMFUF 3BJMTͰ͸جຊతʹϨίʔυͣͭૢ࡞Λߦ͏ ղܾํ๏ɿCVMLͰॲཧ͢Δ ˞DBMMCBDLͷॲཧͳͲ͕ཁ஫ҙ 

  13. σʔλΛऔΓ௚ͯ͠͠·͏ ͍࣋ͬͯΔσʔλͳͷʹɺΫΤϦ͕ൃߦ͞ΕΔ ͋ͪͪ͜Ͱಉ͡Πϯελϯε͕Ͱ͖ͯ͠·͏ ղܾํ๏ɿखಈͰೖΕΔɺΩϟογϡػߏΛ࡞ΔɺͳͲ A大学の学生、 集まってください! (1人ずつに対して) あなたは何大学? 

  14. ͦͷଞɺ/ ͕ى͖͕ͪͳͱ͜Ζ  %#෼ׂ͞Ε͍ͯΔ৔߹ɺͦ΋ͦ΋ɺJODMVEFT͕Ͱ͖ͳ͍  ઈରଘࡏ͠ͳ͍ؔ࿈σʔλΛҾ͜͏ͱ͢Δ  ෳ਺ճJODMVEF͢ΔͱԿ౓΋ΫΤϦ͕ൃߦ͞ΕΔ  Ωϟογϡαʔόʔ΋/

    ͕͋Δ  Ϩίʔυ͕ଟ͗ͯ͢ɺ࠷ॳ͔Βϩʔυͨ͘͠ͳ͍৔߹ ղܾํ๏ɿؤுΔX 
  15. ೉͍͠ͱ͜Ζ େن໛։ൃͳΒɺίʔυͷϝϯςφϯεੑ͕ॏཁࢹ͞ΕΔ ಉ͡ϝιουΛ͋ͪͪ͜Ͱ࠶ར༻͢Δ͜ͱ͕ଟ͍  / ͕ͱͯ΋ى͖͕ͪ  ͦΕͧΕͷίϯςΩετͰɺ/ ͕ى͖ͳ͍͜ͱΛ֬ೝ͢Δඞཁ͕͋Δ 

    ֊૚͕ଟ͍΄Ͳɺ೉͍͠ ղܾ೉қ౓ֻ͕͚ࢉ 
  16. / ΫΤϦΛආ͚Δʹ͸ ׬ᘳͳ։ൃΛ͢Ε͹/ ͳΜͯग़ͳ͍ ʢͦΜͳΘ͚͕ͳ͍ʂʂʣ 3BJMTͷੜ࢈ੑ͕ߴ͍ͷͰɺΞϓϦ͕αΫαΫ࡞ΕΔ ίʔυΛॻ࣌͘఺Ͱආ͚ΒΕͳ͍͜ͱ͕ଟ͍ͷͰ ϦϦʔεલʹ͔ͬ͠ΓνΣοΫ͢Δඞཁ͕͋Δ ؾͮ͘ํ๏ 

    ݕ஌πʔϧ CVMMFU  ϩάͷ֬ೝ  ෛՙࢼݧ  ؂ࢹπʔϧ 
  17. ·ͱΊ ήʔϜαʔόʔ͸%#ʹ༏͍͠։ൃ͕ٻΊΒΕΔ / ΫΤϦʹ஫ҙ͠ͳ͕Β։ൃΛߦ͍ͬͯ·͢ ͝ਗ਼ௌɺ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂʂʂ