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
6
10年物のRailsアプリにキャッチアップ!〜コードを読まずに理解したかった〜
ima1zumi
0
20
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
Identifying User Idenity
moro
6
9.8k
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
230
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
560
CSC509 Lecture 12
javiergs
PRO
0
140
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
Amazon Qを使ってIaCを触ろう!
maruto
0
390
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
210
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
310
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
5
1.5k
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
390
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
250
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
170
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
32
1.5k
What's in a price? How to price your products and services
michaelherold
243
12k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
A Modern Web Designer's Workflow
chriscoyier
693
190k
Agile that works and the tools we love
rasmusluckow
327
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Automating Front-end Workflow
addyosmani
1366
200k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Happy Clients
brianwarren
98
6.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
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