Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Railsでフォロー機能を作るためのモデルの書き方
Search
ima1zumi
May 09, 2020
Programming
1
720
Railsでフォロー機能を作るためのモデルの書き方
This is the presentation I gave at the "First LT Meeting #2" for the students on May 9, 2020.
ima1zumi
May 09, 2020
Tweet
Share
More Decks by ima1zumi
See All by ima1zumi
Exploring Reline: Enhancing Command Line Usability
ima1zumi
0
7
10年物のRailsアプリにキャッチアップ!〜コードを読まずに理解したかった〜
ima1zumi
0
21
RubyKaigiの登壇者一覧ページを作った
ima1zumi
0
200
Relineのその後の生活
ima1zumi
0
160
IRB and Reline Kaigi 2024
ima1zumi
0
7
Exploring Reline: Enhancing Command Line Usability
ima1zumi
3
13k
Reline 1分 Cooking
ima1zumi
0
21
続・mruby/cにUTF-8 を実装する
ima1zumi
1
21
UTF-8 is coming to mruby/c
ima1zumi
4
5.2k
Other Decks in Programming
See All in Programming
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
4
860
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.3k
CSC509 Lecture 09
javiergs
PRO
0
140
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
5
1.7k
役立つログに取り組もう
irof
28
9.5k
Jakarta EE meets AI
ivargrimstad
0
440
受け取る人から提供する人になるということ
little_rubyist
0
220
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
JavaでLチカしたい! / JJUG CCC 2024 Fall LT
nhayato
0
130
Quine, Polyglot, 良いコード
qnighy
4
640
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
120
CSC509 Lecture 12
javiergs
PRO
0
140
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Designing Experiences People Love
moore
138
23k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Documentation Writing (for coders)
carmenintech
65
4.4k
GraphQLとの向き合い方2022年版
quramy
43
13k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9k
Navigating Team Friction
lara
183
14k
4 Signs Your Business is Dying
shpigford
180
21k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
630
The Cult of Friendly URLs
andyhume
78
6k
Transcript
RailsͰϑΥϩʔػೳΛ࡞ΔͨΊ ͷϞσϧͷॻ͖ํ ϑΟϤϧυϒʔτΩϟϯϓ ΦϯϥΠϯLTձ ima1zumi 2020-05-09
ࣗݾհ - @ima1zumiʢࠓઘ ຑཬʣ - ҎલSIerͰCOBOLͱ͔DB৮ͬͯ·ͨ͠ - Rails ϢʔβʔϑΥϩʔͷϓϥΫςΟεத -
࠷ۙͷझຯɿ͋ͭɺίʔώʔɺࣗ։ൃڥΛ͍͍ײ͡ʹ͢Δ͜ͱ
ςʔϚΛબΜͩཧ༝ - ϢʔβʔϑΥϩʔͰϞσϧʹॻ͘belongs_toͱ͔ has_many͕ढจʹݟ͑Δ - ಡΈํ͕͔Βͳͯ͘Μͩ
͜Ε͔Β͢͜ͱ - ϑΥϩʔػೳΛ࡞ΔʹɺσʔλϕʔεΛͲ͏࡞Ε ͍͍ͷ͔ʢσʔλϕʔεͷʣ - ͦΕΛRailsͰͲ͏ఆٛ͢Δͷ͔ʢRailsͷʣ
ϑΥϩʔɾϑΥϩϫʔͷؔ ͪΌͪΌ·ΔδϡϯΛϑΥϩʔ͍ͯ͠Δ(following) δϡϯͪΌͪΌ·ΔʹϑΥϩʔ͞Ε͍ͯΔ (followed) ͪΌͪΌ·Δ δϡϯ following followed
ྫɿϢʔβʔද JE OBNF ͪΌͪΌ·Δ αϒϨ δϡϯ
ϑΥϩʔΛ࣮͢Δʁ JE OBNF GPMMPXFS@JE GPMMPXFE@JE ͪΌͪΌ·Δ αϒϨ
δϡϯ
JE OBNF GPMMPXFS@JE GPMMPXFE@JE ͪΌͪΌ·Δ
αϒϨ δϡϯ - 1ͭͷηϧʹෳͷ͕ೖΔ - ֤ྻͷΛҰҙʹܾΊΒΕͳ͘ͳΔͷͰɺDBͰNG
2ͭͷςʔϒϧΛ͏ 6TFS JE OBNF ͪΌͪΌ·Δ αϒϨ δϡϯ
GSJFOETIJQ GPMMPXFS@JE GPMMPXFE@JE
RailsͰ͜ͷؔΛදݱ͢Δʹʁ - RailsͰɺΞϓϦέʔγϣϯ͕ѻ͏σʔλͱखଓ͖ Ϟσϧʹॻ͘ - UserϞσϧͱFriendshipϞσϧ - ̎ͭͷϞσϧͷؒʹ͕ؔ͋Δ͜ͱΛఆٛ͢Δ - belongs_to,
has_manyΛ͏
belongs_toؔ࿈͚ - ࣗͷϞσϧͱ૬खͷϞσϧ͕1:1ͷؔͰ͋Δ - ଞํͷϞσϧʹॴଐ(belongs_to)͢Δ - book ͔Βݟͯ author(ஶऀ)ඞͣ1ਓ Ҿ༻ɿIUUQTSBJMTHVJEFTKQBTTPDJBUJPO@CBTJDTIUNM
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ͷؔ
has_manyؔ࿈͚ Ҿ༻ɿIUUQTSBJMTHVJEFTKQBTTPDJBUJPO@CBTJDTIUNM - ࣗͷϞσϧͱ૬खͷϞσϧͱ͕1:ଟͷؔͰ͋Δ - ૬खͷϞσϧΛॴ༗(has_many)͢Δ - author ෳͷ book
Λ࣋ͪ͏Δ
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ଟͷؔ
UserϞσϧ(following) 2 class User < ApplicationRecord ... has_many :following, #
カラム名 through: :active_relationships, # active_relationshipsを通して source: :followed # 関連付け先モデル名 ... end 6TFS͕BDUJWF@SFMBUJPOTIJQTΛ௨ͯ͠ GPMMPXFEͱଟɿଟͷؔΛ࡞Δ
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 ͱܗΛ͋ΘͤΔͨΊʹॻ͍ͯ͋Δ
σʔλͷؔ࿈͚͕Ͱ͖ͨ ϑΥϩʔػೳΛ࣮͢Δʹfollow/unfollowϝιου Λ࡞ͬͨΓɺ ίϯτϩʔϥͱϏϡʔΛ࣮ͨ͠Γ͢Δඞཁ͕͋Δ ͕ɺ ͜ͷLTͰׂѪ
Ϟσϧಉ࢜ͷؔhas_many, belongs_toͳͲͰදͤ Δ Ϟσϧ໊ͷݴ͍͑Λͯ͠ɺUserͱFriendshipʹ followerͱfollowedͷ2ͭͷؔΛ࡞Δ ·ͱΊ
ࢀߟจݙͳͲ ୡਓʹֶͿ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