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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
ryonext
February 20, 2013
Programming
1k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
850
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.2k
CircleCIとwercker
ryonext
3
1.3k
Other Decks in Programming
See All in Programming
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
160
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
130
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
480
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
310
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
420
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
2
330
その節約、円になってますか?
isamumumu
1
640
React本体のコードリーディング
high_g_engineer
1
120
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
300
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
190
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
460
テーブルをDELETEした
yuzneri
0
130
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
119
120k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
420
How to make the Groovebox
asonas
2
2.3k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.3k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
500
RailsConf 2023
tenderlove
30
1.5k
[SF Ruby Conf 2025] Rails X
palkan
2
1.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
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文走るよ ね、ってことを思った。