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
820
Other Decks in Technology
See All in Technology
SREの次のキャリアの道しるべ 〜SREがマネジメントレイヤーに挑戦して、 気づいたこととTips〜
coconala_engineer
1
1.1k
NewSQLや分散データベースを支えるRaftの仕組み - 仕組みを理解して知る得意不得意
hacomono
PRO
3
230
freeeのアクセシビリティの現在地 / freee's Current Position on Accessibility
ymrl
2
280
「Chatwork」のEKS環境を支えるhelmfileを使用したマニフェスト管理術
hanayo04
1
250
ビジネス職が分析も担う事業部制組織でのデータ活用の仕組みづくり / Enabling Data Analytics in Business-Led Divisional Organizations
zaimy
1
310
助けて! XからWaylandに移行しないと新しいGNOMEが使えなくなっちゃう 2025-07-12
nobutomurata
2
140
Zero Data Loss Autonomous Recovery Service サービス概要
oracle4engineer
PRO
2
7.8k
推し書籍📚 / Books and a QA Engineer
ak1210
0
120
QuickSight SPICE の効果的な運用戦略~S3 + Athena 構成での実践ノウハウ~/quicksight-spice-s3-athena-best-practices
emiki
0
260
ABEMAの本番環境負荷試験への挑戦
mk2taiga
5
830
Sansanのデータプロダクトマネジメントのアプローチ
sansantech
PRO
0
230
ポストコロナ時代の SaaS におけるコスト削減の意義
izzii
1
270
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Building Applications with DynamoDB
mza
95
6.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Balancing Empowerment & Direction
lara
1
440
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.4k
Done Done
chrislema
184
16k
Embracing the Ebb and Flow
colly
86
4.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
Designing for Performance
lara
610
69k
Code Review Best Practice
trishagee
69
19k
Raft: Consensus for Rubyists
vanstee
140
7k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
700
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
Ϋϥεͱͯ͠ඞཁ ≠ ςʔϒϧ࡞Δ
ݴΘΕΔͱવͳΜ͚ͩͲ ҰਓͰͬͯΔͱҊ֎ϛε͍ͬͯΔ
·ͱΊ
·ͱΊ • ΞϓϦέʔγϣϯͷϑϨʔϜϫʔΫ͕มΘΔ͜ͱ͋ͬͯ σʔλϕʔε͕·ΔͬͱมΘΔ͜ͱͳ͔ͳ͔ͳ͍ɻ • ͔ͩΒͦ͜ɺσʔλϕʔεઃܭͳͲΛ͓Ζ͔ͦʹͤͣɺͪΌ ΜͱΔඞཁ͕͋Δɻ • ͨͩɺઃܭҰਓͰͬͯΔͱ͍ͭͷؒʹ͔ϋϚͬͯͨΓਏ͍ ͷͰɺڧ͍ਓͱҰॹʹͱ͔ɺଟਓͰͬͨ΄͏͕Αͦ͞͏ɻ