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

Rails講義 第25回 model

Rails講義 第25回 model

Kuniaki IGARASHI

December 11, 2012
Tweet

More Decks by Kuniaki IGARASHI

Other Decks in Technology

Transcript

  1. JOEFY CPPLT (&5 OFX CPPLTOFX (&5 FEJU CPPLTJEFEJU (&5 TIPX

    (&5 CPPLTJE DSFBUF 1045 CPPLT VQEBUF CPPLTJE EFTUSPZ %&-&5& CPPLTJE ৽نొ࿥ SFEJSFDU SFEJSFDU ߋ৽ ฤू SFEJSFDU ৽نೖྗ ࡟আ ΞΫγϣϯ໊ )551ϝιου VSM ΞΫγϣϯ໊ )551ϝιου VSM ͜ͷਤ ͷݟํ ը໘͋Γ ը໘ͳ͠ $36%ը໘ભҠਤ 165 ίί‎ Ϟσϧ͸ͨͱ͑͹͜͜ʹ͍·͢ɻ
  2. BQQDPOUSPMMFSTCPPLT@DPOUSPMMFSSC EFGDSFBUF !CPPL#PPLOFX QBSBNT<CPPL> SFTQPOE@UPEPcGPSNBUc JG!CPPLTBWF GPSNBUIUNM\SFEJSFDU@UP!CPPL OPUJDF#PPLXBTTVDDFTTGVMMZDSFBUFE^ GPSNBUKTPO\SFOEFSKTPO!CPPL TUBUVTDSFBUFE

    MPDBUJPO!CPPL^ FMTF GPSNBUIUNM\SFOEFSBDUJPOOFX^ GPSNBUKTPO\SFOEFSKTPO!CPPLFSSPST TUBUVTVOQSPDFTTBCMF@FOUJUZ^ FOE FOE FOE ϦΫΤετ 63-IUUQMPDBMIPTUCPPLT )551ϝιουɿ1045 ύϥϝʔλ 'PSNEBUB ɿ CPPL<UJUMF>େ౦ژτΠϘοΫε CPPL<NFNP>ήʔϜ։ൃձࣾΛ෣୆ʹͨ͠೤݂ετʔϦʔ ϦΫΤετͷύϥϝʔλΛ࢖ͬͯ ຊͷσʔλΛ࡞Δ ຊͷσʔλΛอଘ͢Δ B੒ޭͨ͠ΒTIPXը໘΁ Cอଘࣦഊͨ͠ΒOFXը໘΁ ͖ͬ͞ͷը໘ #PPLT$POUSPMMFSͷDSFBUFΞΫγϣϯ
  3. BQQDPOUSPMMFSTCPPLT@DPOUSPMMFSSC EFGDSFBUF !CPPL#PPLOFX QBSBNT<CPPL> SFTQPOE@UPEPcGPSNBUc JG!CPPLTBWF GPSNBUIUNM\SFEJSFDU@UP!CPPL OPUJDF#PPLXBTTVDDFTTGVMMZDSFBUFE^ GPSNBUKTPO\SFOEFSKTPO!CPPL TUBUVTDSFBUFE

    MPDBUJPO!CPPL^ FMTF GPSNBUIUNM\SFOEFSBDUJPOOFX^ GPSNBUKTPO\SFOEFSKTPO!CPPLFSSPST TUBUVTVOQSPDFTTBCMF@FOUJUZ^ FOE FOE FOE ϦΫΤετ 63-IUUQMPDBMIPTUCPPLT )551ϝιουɿ1045 ύϥϝʔλ 'PSNEBUB ɿ CPPL<UJUMF>େ౦ژτΠϘοΫε CPPL<NFNP>ήʔϜ։ൃձࣾΛ෣୆ʹͨ͠೤݂ετʔϦʔ ϦΫΤετͷύϥϝʔλΛ࢖ͬͯ ຊͷσʔλΛ࡞Δ ຊͷσʔλΛอଘ͢Δ B੒ޭͨ͠ΒTIPXը໘΁ Cอଘࣦഊͨ͠ΒOFXը໘΁ ͖ͬ͞ͷը໘ #PPL.PEFM͕อଘͷ࢓ࣄΛ͍ͯ͠·ͨ͠ɻ ‐#PPLΫϥε͸.PEFMͱ͍͏छ଒ʹଐ͍ͯ͠·͢ɻ ‐͜͜Ͱอଘ͍ͯ͠·͢ɻ
  4. CPPLT#PPLBMM جຊతͳϞσϧͷ࢖͍ํಡΈग़͠ฤ #PPLBMMͰอଘ͞Ε͍ͯΔ#PPL.PEFMͷશσʔλΛऔಘͰ͖·͢ɻ ʢҎલʹઆ໌ͨ͠Ұཡը໘ JOEFYΞΫγϣϯ Ͱ͜ͷϝιου͕࢖ΘΕ͍ͯ·͢ʣ #PPLBMM͢Δͱ഑ྻʹ#PPLΦϒδΣΫτ͕٧·ͬͯฦ͖ͬͯ·͢ɻ CPPL#PPLXIFSF UJUMFϋνϛπͱΫϩʔόʔ CPPLUJUMF‎ϋνϛπͱΫϩʔόʔ

    CPPLNFNP‎ඒେΛ෣୆ʹͨ͠੨य़ϥϒίϝ XIFSFϝιουΛ࢖͏ͱݕࡧ͕Ͱ͖·͢ɻ λΠτϧ͕ϋνϛπͱΫϩʔόʔͰ͋Δ#PPLΦϒδΣΫτ͕ฦΓ·͢ɻ #PPLΦϒδΣΫτ͸UJUMFϝιουͰλΠτϧΛɺ NFNPϝιουͰϝϞΛͦΕͧΕฦ͠·͢ɻ ͖͞΄Ͳอଘͨ͠σʔλΛಡΈऔͬͯΈ·͠ΐ͏ɻ
  5. CPPL#PPLOFX UJUMF޷͖ͳλΠτϧ NFNP޷͖ͳίϝϯτ ࣮शSBJMTDPOTPMFͰϞσϧΛ࢖ͬͯΈΔ CPPLTBWF ‏อଘ͠·͢ 3BJMTʹ͸3VCZͰ͍͏JSCͷΑ͏ͳɺ ߦͣͭίʔυΛ࣮ߦ͢Δػೳ͕͋Γ·͢ɻ CVOEMFFYFDSBJMTD Q‏͜͜ʹίʔυΛଧͪ·͢ɻ

    #PPLMBTU ‏࠷ޙͷσʔλΛऔಘ͠·͢ ্Ͱอଘͨ͠஋ͷ֬ೝ ‏վߦ͸ෆཁ IUUQMPDBMIPTUCPPLT ϒϥ΢βͰΞΫηε DECPPLT@BQQ CVOEMFFYFDSBJMTD ‏D͸DPOTPMFͷུ ·ͨɺ͜͜Ͱอଘͨ͠σʔλ͸ϒϥ΢β͔Β΋ݟΔ͜ͱ͕Ͱ͖·͢ɻ CVOEMFFYFDSBJMTT SBJMTαʔόΛىಈ લ४උͰͭͬͨ͘ΞϓϦ΁Ҡಈͯ͠ɺSBJMTDPOTPMFΛ࢖ͬͯΈ·͠ΐ͏ɻ ‏3BJMT3PPUϑΥϧμ΁Ҡಈ ҎԼͷίʔυΛ࣮ߦͯ͠Έ͍ͯͩ͘͞ɻ
  6. %#ઃܭਤNJHSBUJPO DMBTT$SFBUF#PPLT"DUJWF3FDPSE.JHSBUJPO EFGDIBOHF DSFBUF@UBCMFCPPLTEPcUc UTUSJOHUJUMF UUFYUNFNP UUJNFTUBNQT FOE FOE FOE

    BQQECNJHSBUF@DSFBUF@CPPLTSC %#ͷઃܭਤΛNJHSBUJPO ϚΠάϨʔγϣϯ ϑΝΠϧͱݺͼ·͢ɻ NJHSBUJPO΋3BJMT 3VCZ ͷίʔυͰॻ͔Ε͍ͯ·͢ɻ
  7. %#ઃܭਤNJHSBUJPO DMBTT$SFBUF#PPLT"DUJWF3FDPSE.JHSBUJPO EFGDIBOHF DSFBUF@UBCMFCPPLTEPcUc UTUSJOHUJUMF UUFYUNFNP UUJNFTUBNQT FOE FOE FOE

    BQQECNJHSBUF@DSFBUF@CPPLTSC CPPLTͱ͍͏໊લͷςʔϒϧΛ࡞Γ·͢ɻ%#͸ςʔϒϧͱ͍͏୯ҐͰ σʔλΛ؅ཧ͠·͢ɻ͜ͷΞϓϦͷຊʹؔ͢ΔσʔλΛอଘ͢ΔͨΊ ʹɺCPPLTͱ͍͏໊લͷςʔϒϧΛ࡞͍ͬͯ·͢ɻ ˞ςʔϒϧ໊͸Ϟσϧ໊ͷෳ਺ܗʹ͢Δͱ͍͏ݪଇ͕͋Γ·͢ɻ
  8. %#ઃܭਤNJHSBUJPO DMBTT$SFBUF#PPLT"DUJWF3FDPSE.JHSBUJPO EFGDIBOHF DSFBUF@UBCMFCPPLTEPcUc UTUSJOHUJUMF UUFYUNFNP UUJNFTUBNQT FOE FOE FOE

    BQQECNJHSBUF@DSFBUF@CPPLTSC CPPLTςʔϒϧ͸UJUMFͱ͍͏ཁૉͱɺNFNPͱ͍͏ཁૉΛ࣋ͪ·͢ɻ ʢ͜ͷཁૉͷ͜ͱΛ%#༻ޠͰΧϥϜͱ͍͍·͢ɻʣ ·ͨɺ࡞੒೔࣌΍ߋ৽೔࣌Λه࿥͢ΔཁૉΛ࣋ͪ·͢ɻ UUJNFTUBNQT͕࡞੒͠·͢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HSBUF@DSFBUF@CPPLTSC NJHSBUJPOϑΝΠϧ %# CPPLTςʔϒϧ UJUMF NFNP DSFBUFE@BU VQEBUFE@BU CVOEMFFYFDSBLFECNJHSBUF NJHSBUJPOϑΝΠϧ %#ઃܭਤ ͔Β%#Λ࡞Δ DMBTT#PPL"DUJWF3FDPSE#BTF BUUS@BDDFTTJCMFNFNP UJUMF FOE BQQNPEFMTCPPLSC NPEFMϑΝΠϧ ϑΝΠϧੜ੒ ϑΝΠϧੜ੒ Ͱ͖ͨNPEFMϑΝΠϧͱ%#Λར༻ͯ͠ΞϓϦ͸ಈ͖·͢ɻ
  18. طଘͷ%#ςʔϒϧ .PEFM ʹΧϥϜΛ૿΍͢ʹ͸ʁ CVOEMFFYFDSBJMTHNJHSBUJPO"EE"VUIPS5P#PPLTBVUIPSTUSJOH SBJMTHίϚϯυʹNJHSBUJPOΛࢦఆ͢ΔͱNJHSBUJPOϑΝΠϧ͚ͩ Λੜ੒Ͱ͖·͢ɻ ͨͱ͑͹ɺCPPLTςʔϒϧʹTUSJOHܕͷBVUIPSΛՃ͑Δʹ͸ҎԼͷΑ͏ʹ͠·͢ɻ NJHSBUJPOϑΝΠϧੜ੒ ‏CPPLTςʔϒϧʹBVUIPSΛTUSJOHܕͰ௥Ճ ͷҙ

    DMBTT"EE"VUIPS5P#PPLT"DUJWF3FDPSE.JHSBUJPO EFGDIBOHF BEE@DPMVNOCPPLT BVUIPS TUSJOH FOE FOE CVOEMFFYFDSBLFECNJHSBUF %# CPPLTςʔϒϧ UJUMF NFNP BVUIPS DSFBUFE@BU VQEBUFE@BU NJHSBUJPO͔Β%#Λ࡞Δ ECNJHSBUF@BEE@BVUIPS@UP@CPPLTSC
  19. ͪͳΈʹɺ3BJMTͷHFOFSBUFػೳ͸։ൃΛΞγετ͢Δػೳͳ ͷͰɺ࢖Θͳ͍Ͱ͔ΒखͰίʔυΛॻ͍ͯ΋ಉ༷ʹಈ͖·͢ɻ CVOEMFFYFDSBJMTHNJHSBUJPO"EE"VUIPS5P#PPLTBVUIPSTUSJOH NJHSBUJPOϑΝΠϧੜ੒ DMBTT"EE"VUIPS5P#PPLT"DUJWF3FDPSE.JHSBUJPO EFGDIBOHF BEE@DPMVNOCPPLT BVUIPS TUSJOH FOE

    FOE ECNJHSBUF@BEE@BVUIPS@UP@CPPLTSC ํ๏"SBJMTHΛ࢖͏ํ๏ DMBTT"EE"VUIPS5P#PPLT"DUJWF3FDPSE.JHSBUJPO EFGDIBOHF BEE@DPMVNOCPPLT BVUIPS TUSJOH FOE FOE ECNJHSBUF@BEE@BVUIPS@UP@CPPLTSC ํ๏#͔ΒखͰίʔυΛॻ͘ํ๏ ΤσΟλͰ͜ͷϑΝΠϧ໊Ͱ ‏͜ͷ಺༰Λॻ͘ ͨͱ͑͹ɺҎԼͷํ๏"ͱํ๏#ɺͲͪΒ΋ಉ͡Α͏ʹಈ࡞͠·͢ɻ ΄͔ͷSBJMTHίϚϯυɺTDBGGPME΍DPOUSPMMFSͳͲ΋શͯಉ͜͡ͱ͕ݴ͑·͢ɻ