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
良いアプリケーションは良いデータベースに宿る
Search
Ya-Suzuki
November 27, 2019
Technology
2
300
良いアプリケーションは良いデータベースに宿る
2019/11/27 Classi Rails Night
Ya-Suzuki
November 27, 2019
Tweet
Share
More Decks by Ya-Suzuki
See All by Ya-Suzuki
突撃!隣のフォームオブジェクト
y_s___3128
1
810
Other Decks in Technology
See All in Technology
Amazon S3標準/ S3 Tables/S3 Express One Zoneを使ったログ分析
shigeruoda
2
380
CIでのgolangci-lintの実行を約90%削減した話
kazukihayase
0
340
Agentic DevOps時代の生存戦略
kkamegawa
0
950
Observability infrastructure behind the trillion-messages scale Kafka platform
lycorptech_jp
PRO
0
130
LinkX_GitHubを基点にした_AI時代のプロジェクトマネジメント.pdf
iotcomjpadmin
0
160
初めてのAzure FunctionsをClaude Codeで作ってみた / My first Azure Functions using Claude Code
hideakiaoyagi
1
180
IIWレポートからみるID業界で話題のMCP
fujie
0
720
白金鉱業Meetup_Vol.19_PoCはデモで語れ!顧客の本音とインサイトを引き出すソリューション構築
brainpadpr
2
470
25分で解説する「最小権限の原則」を実現するための AWS「ポリシー」大全 / 20250625-aws-summit-aws-policy
opelab
6
700
2年でここまで成長!AWSで育てたAI Slack botの軌跡
iwamot
PRO
2
140
VCpp Link and Library - C++ breaktime 2025 Summer
harukasao
0
220
Amplifyとゼロからはじめた AIコーディング 成果と展望
mkdev10
1
360
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
77
9.4k
Become a Pro
speakerdeck
PRO
28
5.4k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
How to Ace a Technical Interview
jacobian
277
23k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
Into the Great Unknown - MozCon
thekraken
39
1.9k
What's in a price? How to price your products and services
michaelherold
245
12k
Transcript
ྑ͍ΞϓϦέʔγϣϯ ྑ͍σʔλϕʔεʹ॓Δ 2019.11.27 Classi Rails Night ླ߁म(@y_s____731)
• ླ߁म • AGBIOTECHגࣜձࣾ • Ruby/Rails/ΤϯδχΞྺ 3 • લ৬T̋Y̋TAͰઃඋͷमཧͯ͠·ͨ͠ɻ •
twitter @y_s_____731
લஔ͖
σʔλϕʔεɾɾɾ • ʑ͞ΘͬͯΔͣ • ҙࣝͤͣͱ͞ΘΕͯ͠·͏ • RailsʢActiveRecordʣͷੌ͞
ͳ͔ͳ͔ݴٴ͞Εͳ͍
͍͟ຊ൪ڥˠແࣄࢮ
σʔλϕʔεΛΒͣʹ ྑ͍ΞϓϦέʔγϣϯ࡞Εͳ͍
None
Classi͞ΜͷΠϕϯτ͕ ݸਓతʹλʔχϯάϙΠϯτ
1.໊લॏཁ
ͦΜͳ͜ͱ͋ͬͯɺࢲͷઃܭ্ͷ࠲ӈͷʮ໊લॏཁʯ Ͱ͢ɻ ͋ΒΏΔػೳΛσβΠϯ͢Δ࣌ʹɺࢲͦͷ໊લʹͬͱ ͩ͜ΘΓ·͢ɻ ϓϩάϥϚͱͯ͠ͷΩϟϦΞͷதͰɺదͳ໊લΛ͚ͭΔ͜ ͱ͕Ͱ͖ͨػೳޭ͠ɺͦ͏Ͱͳ͍ػೳʹ͍ͭͯޙͰޙ չ͢Δ͜ͱ͕ଟ͔ͬͨΑ͏ʹࢥ͏͔ΒͰ͢ɻ by Matz
σʔλϕʔεಉ͡
σʔλϕʔεʹ͓͚Δ໊લɾɾɾ • ςʔϒϧ໊ • ΧϥϜ໊
୭͕Ͳ͏ΈͯɺԿΛදͯ͠Δ͔͔Δ
ந× ۩ମత̋ ʢUser → Teature, Studentͱ͔ʣ நతͳ໊લ͕ᐆດʹͳΓ͕ͪ ↪︎FatModelʹͳΓ͍͢
ུ͞ͳ͍ ʢྫ.༵ dow× day_of_week̋ʣ ಛʹαʔϏεݻ༗ͷݴ༿ͳͲུ͞ΕΔͱ ޙ͔Βೖͬͨਓ͖ͬͱਏ͍
2.੍Λ׆༻͢Δ
੍ RDBMS͕ඪ४Ͱ͍࣋ͬͯΔ ೖྗΛ੍ݶ͢Δػೳ
੍ͷछྨɾɾɾ • NOT NULL੍ • ForeignKey(֎෦Ωʔ)੍ • UNIQUE੍ • CHECK੍…etc
NOT NULL ੍ NULLΛېࢭ͢Δ
class CreateArticles < ActiveRecord::Migration[5.2] def change create_table :articles do |t|
t.string :title, null: false t.text :content, null: false t.timestamps end end end
࠷ॳʹ͚ͭΔͷ؆୯ɻ Ͱɺޙ͔ΒՃ͠ਏ͍͜ͱɻ
جຊతʹNOT NULL͚ͭΔ ↓ Կ͕NULLABLEͳͷ͔Λߟ͑Δ
֎෦Ωʔ ੍ ผͷςʔϒϧͷಛఆͷΧϥϜʹ͋Δ͔͠ ೖྗͰ͖ͳ͍Α͏ʹ͢Δػೳ
Α͘͏ͷதؒςʔϒϧ ϑΥϩʔɾϑΥϩϫʔΈ͍ͨͳؔ࿈͚Ͱ ଘࡏ͠ͳ͍ϢʔβʔΛϑΥϩʔͰ͖ͳ͍Α͏ʹ͢Δ
None
class CreateRelationships < ActiveRecord::Migration[5.2] def change create_table :relationships do |t|
t.references :user, foreign_key: true, null: false t.references :followed, foreign_key: { to_table: :users }, null: false t.timestamps end end end
ΧϥϜ໊ͱࢀরઌͷ໊લ͕ಉ͡ foreign_key: true ҧ͏߹ foreign_key: { to_table: :ςʔϒϧ໊ }
DBͷ߹ੑΛอͭ࠷ޙͷࡆ = ੍
੍ͪΌΜͱ͍·͠ΐ͏
3.ΞϓϦέʔγϣϯ͋Γ͖ͰDBΛߟ͑ͳ͍
Railsͷಛ ςʔϒϧ = Ϋϥε
ςʔϒϧઃܭͱ࣮Λಉ࣌ʹߟ͕͑ͪ
ҰͭͷΞΧϯτʹෳͷϩʔϧ͕͋Δ ྫ.ചΓखͱങ͍ख
None
class User < ApplicationRecord # ڞ௨ͷใ end class Seller <
ApplicationRecord has_many :selling_items, dependent: :destroy ɹ# ചΓखʹؔ͢ΔϦϨʔγϣϯ # ചΓखʹؔ͢ΔϏδωεϩδοΫ end class Buyer < ApplicationRecord # ങ͍खʹؔ͢ΔϦϨʔγϣϯ # ങ͍खʹؔ͢ΔϏδωεϩδοΫ end
త ϦϨʔγϣϯͱϏδωεϩδοΫͷׂ ↓ ςʔϒϧ࡞Δͷ
None
class User < ApplicationRecord # ڞ௨ͷใ end class Seller <
User has_many :selling_items, dependent: :destroy ɹ# ചΓखʹؔ͢ΔϦϨʔγϣϯ # ചΓखʹؔ͢ΔϏδωεϩδοΫ end class Buyer < User # ങ͍खʹؔ͢ΔϦϨʔγϣϯ # ങ͍खʹؔ͢ΔϏδωεϩδοΫ end
Ϋϥεͱͯ͠ඞཁ ≠ ςʔϒϧ࡞Δ
ݴΘΕΔͱવͳΜ͚ͩͲ ҰਓͰͬͯΔͱҊ֎ϛε͍ͬͯΔ
·ͱΊ
·ͱΊ • ΞϓϦέʔγϣϯͷϑϨʔϜϫʔΫ͕มΘΔ͜ͱ͋ͬͯ σʔλϕʔε͕·ΔͬͱมΘΔ͜ͱͳ͔ͳ͔ͳ͍ɻ • ͔ͩΒͦ͜ɺσʔλϕʔεઃܭͳͲΛ͓Ζ͔ͦʹͤͣɺͪΌ ΜͱΔඞཁ͕͋Δɻ • ͨͩɺઃܭҰਓͰͬͯΔͱ͍ͭͷؒʹ͔ϋϚͬͯͨΓਏ͍ ͷͰɺڧ͍ਓͱҰॹʹͱ͔ɺଟਓͰͬͨ΄͏͕Αͦ͞͏ɻ