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
55
10年物のRailsアプリにキャッチアップ!〜コードを読まずに理解したかった〜
ima1zumi
0
65
RubyKaigiの登壇者一覧ページを作った
ima1zumi
0
280
Relineのその後の生活
ima1zumi
0
200
IRB and Reline Kaigi 2024
ima1zumi
0
7
Exploring Reline: Enhancing Command Line Usability
ima1zumi
3
14k
Reline 1分 Cooking
ima1zumi
0
28
続・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
クリーンアーキテクチャから見る依存の向きの大切さ
shimabox
5
1.1k
ソフトウェアエンジニアの成長
masuda220
PRO
12
2.1k
Jasprが凄い話
hyshu
0
180
PRレビューのお供にDanger
stoticdev
1
240
技術を改善し続ける
gumioji
0
140
Better Code Design in PHP
afilina
0
180
Domain-Driven Design (Tutorial)
hschwentner
13
22k
Introduction to kotlinx.rpc
arawn
0
770
良いコードレビューとは
danimal141
1
290
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
160
Jakarta EE meets AI
ivargrimstad
0
510
CloudNativePGを布教したい
nnaka2992
0
120
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
53
13k
A Philosophy of Restraint
colly
203
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
The Language of Interfaces
destraynor
156
24k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
430
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