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
ryonext
February 20, 2013
Programming
2
980
Rubyで階層構造
Rubyで階層構造を実現するancestryというgemの話
ryonext
February 20, 2013
Tweet
Share
More Decks by ryonext
See All by ryonext
AWS Lambda の Ruby 対応
ryonext
0
240
TwitterのList編集しやすいやつ作った
ryonext
0
1.8k
validationについて
ryonext
1
770
AWS Lambda と API GatewayでRails使わずに済んだ話
ryonext
8
4.3k
capistrano-bundle_rsync使ったらオートスケールが高速化した話
ryonext
8
2.5k
PumaとUnicornで最近自分が理解したこと
ryonext
13
9.4k
Hubot事例
ryonext
1
1.6k
Redisでバッチ処理を冗長化しつつ排他制御
ryonext
0
2k
CircleCIとwercker
ryonext
3
1.2k
Other Decks in Programming
See All in Programming
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
3
790
PostgreSQLのRow Level SecurityをPHPのORMで扱う Eloquent vs Doctrine #phpcon #track2
77web
1
100
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
120
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
610
WindowInsetsだってテストしたい
ryunen344
1
190
The Evolution of Enterprise Java with Jakarta EE 11 and Beyond
ivargrimstad
1
850
Using AI Tools Around Software Development
inouehi
0
1.2k
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
1
140
Composerが「依存解決」のためにどんな工夫をしているか #phpcon
o0h
PRO
1
120
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
250
なぜ「共通化」を考え、失敗を繰り返すのか
rinchoku
1
410
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
560
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
107
19k
The World Runs on Bad Software
bkeepers
PRO
69
11k
Six Lessons from altMBA
skipperchong
28
3.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.3k
The Cult of Friendly URLs
andyhume
79
6.4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
228
22k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
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文走るよ ね、ってことを思った。