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
1k
2
Share
Rubyで階層構造
Rubyで階層構造を実現するancestryというgemの話
ryonext
February 20, 2013
More Decks by ryonext
See All by ryonext
AWS Lambda の Ruby 対応
ryonext
0
270
TwitterのList編集しやすいやつ作った
ryonext
0
1.8k
validationについて
ryonext
1
830
AWS Lambda と API GatewayでRails使わずに済んだ話
ryonext
8
4.4k
capistrano-bundle_rsync使ったらオートスケールが高速化した話
ryonext
8
2.6k
PumaとUnicornで最近自分が理解したこと
ryonext
13
9.6k
Hubot事例
ryonext
1
1.7k
Redisでバッチ処理を冗長化しつつ排他制御
ryonext
0
2.1k
CircleCIとwercker
ryonext
3
1.3k
Other Decks in Programming
See All in Programming
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
1
180
決定論 vs 確率論:Gemini 3 FlashとTF-IDFを組み合わせた「法規判定エンジン」の構築
shukob
0
140
個人的に嬉しかったpnpmの新機能・3選
matsuo_atsushi
0
110
Symfony AI in Action - SymfonyLive Berlin 2026
chr_hertel
1
100
Running Swift without an OS
kishikawakatsumi
0
870
How We Practice Exploratory Testing in Iterative Development( #scrumniigata ) / 反復開発の中で、探索的テストをどう実施しているか
teyamagu
PRO
0
290
Claude Code × Gemini × Ebitengine ゲーム制作素人WebエンジニアがGoでゲームを作った話
webzawa
0
210
2026年のソフトウェア開発を考える(2026/05版) / Software Engineering Scrum Fest Niigata 2026 Edition
twada
PRO
18
6.4k
AI時代のPhpStorm最新事情 #phpcon_odawara
yusuke
0
240
Making the RBS Parser Faster
soutaro
0
620
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
260
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.3k
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
360
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
500
Everyday Curiosity
cassininazir
0
200
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Mind Mapping
helmedeiros
PRO
1
170
Raft: Consensus for Rubyists
vanstee
141
7.4k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
53k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.1k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
270
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
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文走るよ ね、ってことを思った。