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
730
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
40
10年物のRailsアプリにキャッチアップ!〜コードを読まずに理解したかった〜
ima1zumi
0
60
RubyKaigiの登壇者一覧ページを作った
ima1zumi
0
240
Relineのその後の生活
ima1zumi
0
190
IRB and Reline Kaigi 2024
ima1zumi
0
7
Exploring Reline: Enhancing Command Line Usability
ima1zumi
3
13k
Reline 1分 Cooking
ima1zumi
0
24
続・mruby/cにUTF-8 を実装する
ima1zumi
1
22
UTF-8 is coming to mruby/c
ima1zumi
4
5.2k
Other Decks in Programming
See All in Programming
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
140
php-conference-japan-2024
tasuku43
0
400
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
200
MCP with Cloudflare Workers
yusukebe
2
260
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
720
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
130
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
230
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
870
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
390
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
1k
fs2-io を試してたらバグを見つけて直した話
chencmd
0
270
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
850
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
BBQ
matthewcrist
85
9.4k
Into the Great Unknown - MozCon
thekraken
34
1.6k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
540
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
4 Signs Your Business is Dying
shpigford
182
21k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
It's Worth the Effort
3n
183
28k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Code Review Best Practice
trishagee
65
17k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
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