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
Rubyで階層構造
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ryonext
February 20, 2013
Programming
2
990
Rubyで階層構造
Rubyで階層構造を実現するancestryというgemの話
ryonext
February 20, 2013
Tweet
Share
More Decks by ryonext
See All by ryonext
AWS Lambda の Ruby 対応
ryonext
0
250
TwitterのList編集しやすいやつ作った
ryonext
0
1.8k
validationについて
ryonext
1
810
AWS Lambda と API GatewayでRails使わずに済んだ話
ryonext
8
4.3k
capistrano-bundle_rsync使ったらオートスケールが高速化した話
ryonext
8
2.5k
PumaとUnicornで最近自分が理解したこと
ryonext
13
9.5k
Hubot事例
ryonext
1
1.6k
Redisでバッチ処理を冗長化しつつ排他制御
ryonext
0
2.1k
CircleCIとwercker
ryonext
3
1.3k
Other Decks in Programming
See All in Programming
2年のAppleウォレットパス開発の振り返り
muno92
PRO
0
190
CSC307 Lecture 05
javiergs
PRO
0
490
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
170
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
160
CSC307 Lecture 01
javiergs
PRO
0
680
Python札幌 LT資料
t3tra
7
1.1k
MUSUBIXとは
nahisaho
0
100
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
400
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
700
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.7k
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
420
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.2k
Featured
See All Featured
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
130
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
160
Practical Orchestrator
shlominoach
191
11k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
230
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
210
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.6k
How to Ace a Technical Interview
jacobian
281
24k
The SEO Collaboration Effect
kristinabergwall1
0
340
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Odyssey Design
rkendrick25
PRO
0
470
Producing Creativity
orderedlist
PRO
348
40k
Transcript
Rubyで階層構造
自己紹介 • Twitter @ryonext • 新横浜の某所から来ました
話の内容 • Rubyで階層構造を実現するancestryという gemの話をします
発端 • デブサミ行きました • t-wadaさんの”SQLアンチパターン”聞きま した • ナイーブツリーの話
親IDだけもつような構造、良くな い • Comments • id • parent_id • comment
• のような設計 • => 深さがどれぐらいになるかわからない し、再起処理でDQNクエリが走る
解決策として • 経路列挙、というのが上げられていた id path comment 1 1/ aaa 2
1/2/ bbb 3 1/2/3/ ccc 4 1/4 ddd
このgem入れれば実装してくれる • https://github.com/stefankroes/ancestry
使い方通りに以下の様なデータを • rails g model group name • Create migration:
rails g migration add_ancestry_to_group ancestry:string • Add index to migration: add_index gruop, :ancestry (UP) / remove_index gruop, :ancestry (DOWN) • Migrate your database: rake db:migrate • Add to app/models/gruop.rb: has_ancestry
こんなデータ作ってみた • 宇宙 • 銀河系 – 太陽系 • 水 •
金 • 地 – 日本 • 関東 • 東京 • 渋谷 • 浅草 • グンマー – アメリカ – イギリス • 火 • 木 • 土 • 天 • 海 • その他 – 冥ちゃん・・・(´;ω;`)
ここで子孫全部取る処理 • 宇宙 < 僕の子孫あつまれー(^o^)ノ • 銀河系 – 太陽系 •
水 • 金 • 地 – 日本 • 関東 • 東京 • 渋谷 • 浅草 • グンマー – アメリカ – イギリス • 火 • 木 • 土 • 天 • 海 • その他 – 冥ちゃん・・・(´;ω;`)
クエリが一個しか発行されない • Group.find_by_name("宇宙").descendants • Group Load (0.4ms) SELECT "groups".* FROM
"groups" WHERE (groups.ancestry like '1/%' or groups.ancestry = '1') • => どの階層でやっても応答速度が同一
Rubyで階層構造 • 階層構造実現したければこれ使えばいいん じゃないでしょうか。 • ファイル/ディレクトリ • 組織階層 • ディスカッションツリー
でも・・・ • 親がよく変わるケース(ディレクトリと か)だと親が変わると鬼update文走るよ ね、ってことを思った。