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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Ya-Suzuki
November 27, 2019
Technology
310
2
Share
良いアプリケーションは良いデータベースに宿る
2019/11/27 Classi Rails Night
Ya-Suzuki
November 27, 2019
More Decks by Ya-Suzuki
See All by Ya-Suzuki
突撃!隣のフォームオブジェクト
y_s___3128
1
850
Other Decks in Technology
See All in Technology
ZOZOTOWNリプレイスでのSkills導入までの流れとこれから.pptx.pdf
zozotech
PRO
2
300
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
3
2.1k
40代からのアウトプット ― 経験は価値ある学びに変わる / 20260404 Naoki Takahashi
shift_evolve
PRO
5
770
QA組織のAI戦略とAIテスト設計システムAITASの実践
sansantech
PRO
1
320
CloudFrontのHost Header転送設定でパケットの中身はどう変わるのか?
nagisa53
1
250
Sansanの認証基盤を支えるアーキテクチャとその振り返り
sansantech
PRO
1
150
Why we keep our community?
kawaguti
PRO
0
390
制約を設計する - 非決定性との境界線 / Designing constraints
soudai
PRO
4
970
The essence of decision-making lies in primary data
kaminashi
0
240
OpenClaw初心者向けセミナー / OpenClaw Beginner Seminar
cmhiranofumio
0
260
Kubernetesの「隠れメモリ消費」によるNode共倒れと、Request適正化という処方箋
g0xu
0
170
Databricks Lakehouse Federationで 運用負荷ゼロのデータ連携
nek0128
0
110
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Music & Morning Musume
bryan
47
7.1k
Google's AI Overviews - The New Search
badams
0
950
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
1
1.5k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
230
Agile that works and the tools we love
rasmusluckow
331
21k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
310
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
43k
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
Ϋϥεͱͯ͠ඞཁ ≠ ςʔϒϧ࡞Δ
ݴΘΕΔͱવͳΜ͚ͩͲ ҰਓͰͬͯΔͱҊ֎ϛε͍ͬͯΔ
·ͱΊ
·ͱΊ • ΞϓϦέʔγϣϯͷϑϨʔϜϫʔΫ͕มΘΔ͜ͱ͋ͬͯ σʔλϕʔε͕·ΔͬͱมΘΔ͜ͱͳ͔ͳ͔ͳ͍ɻ • ͔ͩΒͦ͜ɺσʔλϕʔεઃܭͳͲΛ͓Ζ͔ͦʹͤͣɺͪΌ ΜͱΔඞཁ͕͋Δɻ • ͨͩɺઃܭҰਓͰͬͯΔͱ͍ͭͷؒʹ͔ϋϚͬͯͨΓਏ͍ ͷͰɺڧ͍ਓͱҰॹʹͱ͔ɺଟਓͰͬͨ΄͏͕Αͦ͞͏ɻ