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

Railsでフォロー機能を作るためのモデルの書き方

 Railsでフォロー機能を作るためのモデルの書き方

This is the presentation I gave at the "First LT Meeting #2" for the students on May 9, 2020.

74b5a82b8710accc1e6189a19f0b4935?s=128

ima1zumi

May 09, 2020
Tweet

Transcript

  1. RailsͰϑΥϩʔػೳΛ࡞ΔͨΊ ͷϞσϧͷॻ͖ํ ϑΟϤϧυϒʔτΩϟϯϓ ΦϯϥΠϯLTձ ima1zumi 2020-05-09

  2. ࣗݾ঺հ - @ima1zumiʢࠓઘ ຑཬʣ - Ҏલ͸SIerͰCOBOLͱ͔DB৮ͬͯ·ͨ͠ - Rails ϢʔβʔϑΥϩʔͷϓϥΫςΟεத -

    ࠷ۙͷझຯɿ͋ͭ৿ɺίʔώʔɺࣗ୐։ൃ؀ڥΛ͍͍ײ͡ʹ͢Δ͜ͱ 
  3. ςʔϚΛબΜͩཧ༝ - ϢʔβʔϑΥϩʔͰϞσϧʹॻ͘belongs_toͱ͔ has_many͕ढจʹݟ͑Δ - ಡΈํ͕෼͔Βͳͯ͘೰Μͩ 

  4. ͜Ε͔Β࿩͢͜ͱ - ϑΥϩʔػೳΛ࡞Δʹ͸ɺσʔλϕʔεΛͲ͏࡞Ε ͹͍͍ͷ͔ʢσʔλϕʔεͷ࿩ʣ - ͦΕΛRailsͰͲ͏ఆٛ͢Δͷ͔ʢRailsͷ࿩ʣ

  5. ϑΥϩʔɾϑΥϩϫʔͷؔ܎ ͪΌͪΌ·Δ͸δϡϯΛϑΥϩʔ͍ͯ͠Δ(following) δϡϯ͸ͪΌͪΌ·ΔʹϑΥϩʔ͞Ε͍ͯΔ (followed) ͪΌͪΌ·Δ δϡϯ following followed 

  6. ྫɿϢʔβʔද JE OBNF  ͪΌͪΌ·Δ  αϒϨ  δϡϯ

  7. ϑΥϩʔΛ࣮૷͢Δʁ JE OBNF GPMMPXFS@JE GPMMPXFE@JE  ͪΌͪΌ·Δ   αϒϨ

     δϡϯ  
  8. ໰୊఺ JE OBNF GPMMPXFS@JE GPMMPXFE@JE  ͪΌͪΌ·Δ   

      αϒϨ    δϡϯ   - 1ͭͷηϧʹෳ਺ͷ஋͕ೖΔ - ֤ྻͷ஋ΛҰҙʹܾΊΒΕͳ͘ͳΔͷͰɺDBͰ͸NG 
  9. 2ͭͷςʔϒϧΛ࢖͏ 6TFS JE OBNF  ͪΌͪΌ·Δ  αϒϨ  δϡϯ

    GSJFOETIJQ GPMMPXFS@JE GPMMPXFE@JE         
  10. RailsͰ͜ͷؔ܎Λදݱ͢Δʹ͸ʁ - RailsͰ͸ɺΞϓϦέʔγϣϯ͕ѻ͏σʔλͱखଓ͖ ͸Ϟσϧʹॻ͘ - UserϞσϧͱFriendshipϞσϧ - ̎ͭͷϞσϧͷؒʹؔ܎͕͋Δ͜ͱΛఆٛ͢Δ - belongs_to,

    has_manyΛ࢖͏ 
  11. belongs_toؔ࿈෇͚ - ࣗ෼ͷϞσϧͱ૬खͷϞσϧ͕1:1ͷؔ܎Ͱ͋Δ - ଞํͷϞσϧʹॴଐ(belongs_to)͢Δ - book ͔Βݟͯ author(ஶऀ)͸ඞͣ1ਓ Ҿ༻ɿIUUQTSBJMTHVJEFTKQBTTPDJBUJPO@CBTJDTIUNM

  12. FriendshipϞσϧ class Friendship < ApplicationRecord belongs_to :follower, # 関係の名前 class_name:

    "User" # 関係先モデル名 belongs_to :followed, class_name: "User" end  6TFS JE OBNF  ͪΌͪΌ·Δ  αϒϨ  δϡϯ GSJFOETIJQ GPMMPXFS@JE GPMMPXFE@JE         'SJFOETIJQͱ6TFS͸ͷؔ܎
  13. has_manyؔ࿈෇͚ Ҿ༻ɿIUUQTSBJMTHVJEFTKQBTTPDJBUJPO@CBTJDTIUNM - ࣗ෼ͷϞσϧͱ૬खͷϞσϧͱ͕1:ଟͷؔ܎Ͱ͋Δ - ૬खͷϞσϧΛॴ༗(has_many)͢Δ - author ͸ෳ਺ͷ book

    Λ࣋ͪ͏Δ
  14. UserϞσϧ(following) 1 class User < ApplicationRecord has_many :active_relationships, # Friendshipの言い換え

    class_name: "Friendship", # 関連先クラス名 foreign_key: "follower_id", # 関連するカラム名 dependent: :destroy # 関連先が削除されたときに削除する ... end  6TFS JE OBNF  ͪΌͪΌ·Δ  αϒϨ  δϡϯ GSJFOETIJQ GPMMPXFS@JE GPMMPXFE@JE         6TFSͱ'SJFOETIJQ͸ଟͷؔ܎
  15. UserϞσϧ(following) 2 class User < ApplicationRecord ... has_many :following, #

    カラム名 through: :active_relationships, # active_relationshipsを通して source: :followed # 関連付け先モデル名 ... end 6TFS͕BDUJWF@SFMBUJPOTIJQTΛ௨ͯ͠ GPMMPXFEͱଟɿଟͷؔ܎Λ࡞Δ
  16. UserϞσϧ(followed) class User < ApplicationRecord has_many :passive_relationships, class_name: "Friendship", foreign_key:

    "followed_id", dependent: :destroy has_many :followers, through: :passive_relationships, source: :follower ... end  GPMMPXJOHͱ΄΅ಉ͡ ˞TPVSDFGPMMPXFS͸ඞਢͰ͸ͳ͍͕GPMMPXJOH ͱܗΛ͋ΘͤΔͨΊʹॻ͍ͯ͋Δ
  17. σʔλͷؔ࿈෇͚͕Ͱ͖ͨ ϑΥϩʔػೳΛ࣮૷͢Δʹ͸follow/unfollowϝιου Λ࡞ͬͨΓɺ ίϯτϩʔϥͱϏϡʔΛ࣮૷ͨ͠Γ͢Δඞཁ͕͋Δ ͕ɺ ͜ͷLTͰ͸ׂѪ 

  18. Ϟσϧಉ࢜ͷؔ܎͸has_many, belongs_toͳͲͰදͤ Δ Ϟσϧ໊ͷݴ͍׵͑Λͯ͠ɺUserͱFriendshipʹ followerͱfollowedͷ2ͭͷؔ܎Λ࡞Δ ·ͱΊ 

  19. ࢀߟจݙͳͲ ୡਓʹֶͿDBઃܭ పఈࢦೆॻ https://www.amazon.co.jp/dp/B00EE1XPAI/ RailsΨΠυ Active Record ͷؔ࿈෇͚ https://railsguides.jp/association_basics.html ʲRailsνϡʔτϦΞϧʳϑΥϩʔػೳͷΞιγΤʔγϣϯΛղઆͯ͠ΈΔ

    https://sado-tech.hateblo.jp/entry/2018/08/17/201629 RailsνϡʔτϦΞϧ ୈ14ষ ϢʔβʔΛϑΥϩʔ͢Δ https://railstutorial.jp/chapters/following_users?version=5.1#cha-following_users keynoteςϯϓϨʔτ https://github.com/yulily100/Material-Color-Keynote highlight https://github.com/taggon/highlight