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
740
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
52
10年物のRailsアプリにキャッチアップ!〜コードを読まずに理解したかった〜
ima1zumi
0
64
RubyKaigiの登壇者一覧ページを作った
ima1zumi
0
270
Relineのその後の生活
ima1zumi
0
190
IRB and Reline Kaigi 2024
ima1zumi
0
7
Exploring Reline: Enhancing Command Line Usability
ima1zumi
3
14k
Reline 1分 Cooking
ima1zumi
0
25
続・mruby/cにUTF-8 を実装する
ima1zumi
1
23
UTF-8 is coming to mruby/c
ima1zumi
4
5.3k
Other Decks in Programming
See All in Programming
JavaScriptツール群「UnJS」を5分で一気に駆け巡る!
k1tikurisu
9
1.8k
昭和の職場からアジャイルの世界へ
kumagoro95
1
380
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
120
AIの力でお手軽Chrome拡張機能作り
taiseiue
0
170
Rails アプリ地図考 Flush Cut
makicamel
1
120
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
250
2024年のWebフロントエンドのふりかえりと2025年
sakito
3
250
Unity Android XR入門
sakutama_11
0
160
Ruby on cygwin 2025-02
fd0
0
150
WebDriver BiDiとは何なのか
yotahada3
1
140
1年目の私に伝えたい!テストコードを怖がらなくなるためのヒント/Tips for not being afraid of test code
push_gawa
0
160
法律の脱レガシーに学ぶフロントエンド刷新
oguemon
5
740
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Docker and Python
trallard
44
3.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Producing Creativity
orderedlist
PRO
344
39k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
A designer walks into a library…
pauljervisheath
205
24k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Faster Mobile Websites
deanohume
306
31k
Rails Girls Zürich Keynote
gr2m
94
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
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