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
260
良いアプリケーションは良いデータベースに宿る
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
680
Other Decks in Technology
See All in Technology
開発生産性大幅アップ!Postman VS Code拡張機能
nagix
2
370
コンテナセキュリティの基本と脅威への対策
kyohmizu
3
760
Terraformあれやこれ/terraform-this-and-that
emiki
8
1.4k
アクセス制御にまつわる改善 / Improving access control
itkq
0
530
MapLibreとAmazon Location Service
dayjournal
1
150
TechFeed Experts Night#27 〜 フロントエンドフレームワーク最前線 (Svelte)
baseballyama
1
430
よく聞くけど使ったことないソフトウェアNo.1 KafkaとSnowflake
foursue
4
350
SIEMを用いて、セキュリティログ分析の可視化と分析を実現し、PDCAサイクルを回してみた
coconala_engineer
0
280
Delivering Millions of Messages within seconds @ Duolingo
pelelgrino
0
350
エンジニア候補者向け資料2024.04.24.pdf
macloud
0
3.3k
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
160
ChatGPT for IT Service Management (IT Pro)
dahatake
7
1.6k
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
36
2.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
20
1.9k
4 Signs Your Business is Dying
shpigford
175
21k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
A Philosophy of Restraint
colly
197
16k
Building Better People: How to give real-time feedback that sticks.
wjessup
355
18k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Infographics Made Easy
chrislema
238
18k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
2
3.4k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
Git: the NoSQL Database
bkeepers
PRO
422
63k
Happy Clients
brianwarren
92
6.4k
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
Ϋϥεͱͯ͠ඞཁ ≠ ςʔϒϧ࡞Δ
ݴΘΕΔͱવͳΜ͚ͩͲ ҰਓͰͬͯΔͱҊ֎ϛε͍ͬͯΔ
·ͱΊ
·ͱΊ • ΞϓϦέʔγϣϯͷϑϨʔϜϫʔΫ͕มΘΔ͜ͱ͋ͬͯ σʔλϕʔε͕·ΔͬͱมΘΔ͜ͱͳ͔ͳ͔ͳ͍ɻ • ͔ͩΒͦ͜ɺσʔλϕʔεઃܭͳͲΛ͓Ζ͔ͦʹͤͣɺͪΌ ΜͱΔඞཁ͕͋Δɻ • ͨͩɺઃܭҰਓͰͬͯΔͱ͍ͭͷؒʹ͔ϋϚͬͯͨΓਏ͍ ͷͰɺڧ͍ਓͱҰॹʹͱ͔ɺଟਓͰͬͨ΄͏͕Αͦ͞͏ɻ