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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
ryonext
February 20, 2013
Programming
2
1k
Rubyで階層構造
Rubyで階層構造を実現するancestryというgemの話
ryonext
February 20, 2013
Tweet
Share
More Decks by ryonext
See All by ryonext
AWS Lambda の Ruby 対応
ryonext
0
260
TwitterのList編集しやすいやつ作った
ryonext
0
1.8k
validationについて
ryonext
1
820
AWS Lambda と API GatewayでRails使わずに済んだ話
ryonext
8
4.4k
capistrano-bundle_rsync使ったらオートスケールが高速化した話
ryonext
8
2.5k
PumaとUnicornで最近自分が理解したこと
ryonext
13
9.5k
Hubot事例
ryonext
1
1.7k
Redisでバッチ処理を冗長化しつつ排他制御
ryonext
0
2.1k
CircleCIとwercker
ryonext
3
1.3k
Other Decks in Programming
See All in Programming
メタプログラミングで実現する「コードを仕様にする」仕組み/nikkei-tech-talk43
nikkei_engineer_recruiting
0
150
ご飯食べながらエージェントが開発できる。そう、Agentic Engineeringならね。
yokomachi
1
280
AHC061解説
shun_pi
0
290
登壇資料を作る時に意識していること #登壇資料_findy
konifar
5
2.1k
TipKitTips
ktcryomm
0
150
Rubyと楽しいをつくる / Creating joy with Ruby
chobishiba
0
200
PJのドキュメントを全部Git管理にしたら、一番喜んだのはAIだった
nanaism
0
230
CSC307 Lecture 10
javiergs
PRO
1
690
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
8
2.3k
Event Storming
hschwentner
3
1.3k
AIコーディングの理想と現実 2026 | AI Coding: Expectations vs. Reality 2026
tomohisa
0
890
Rails Girls Tokyo 18th GMO Pepabo Sponsor Talk
yutokyokutyo
0
190
Featured
See All Featured
The Limits of Empathy - UXLibs8
cassininazir
1
240
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
140
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
430
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
92
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
250
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
95
Building Applications with DynamoDB
mza
96
6.9k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
First, design no harm
axbom
PRO
2
1.1k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
760
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文走るよ ね、ってことを思った。