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
960
Rubyで階層構造
Rubyで階層構造を実現するancestryというgemの話
ryonext
February 20, 2013
Tweet
Share
More Decks by ryonext
See All by ryonext
AWS Lambda の Ruby 対応
ryonext
0
220
TwitterのList編集しやすいやつ作った
ryonext
0
1.7k
validationについて
ryonext
1
710
AWS Lambda と API GatewayでRails使わずに済んだ話
ryonext
8
4.2k
capistrano-bundle_rsync使ったらオートスケールが高速化した話
ryonext
8
2.4k
PumaとUnicornで最近自分が理解したこと
ryonext
13
9.3k
Hubot事例
ryonext
1
1.5k
Redisでバッチ処理を冗長化しつつ排他制御
ryonext
0
1.9k
CircleCIとwercker
ryonext
3
1.2k
Other Decks in Programming
See All in Programming
connect-go で面倒くささと戦う / 2024-08-27 #newmo_layerx_go
izumin5210
2
650
Desafios e Lições Aprendidas na Migração de Monólitos para Microsserviços em Java
jessilyneh
2
150
Understand the mechanism! Let's do screenshots tests of Compose Previews with various variations / 仕組みから理解する!Composeプレビューを様々なバリエーションでスクリーンショットテストしよう
sumio
3
790
Perl 5 OOP機構30年史 - Perl 5's OOP Mechanism over the past 30 years
moznion
0
270
Jakarta EE meets AI
ivargrimstad
0
390
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
800
The Shape of a Service Object
inem
0
520
Crafting Cross-Platform Adventures: Building a Game Engine with Kotlin Multiplatform
dwursteisen
0
120
GoのIteratorに詳しくなってしまう
inatonix
1
200
状態管理ライブラリZustandの導入から運用まで
k1tikurisu
3
470
Scala アプリケーションのビルドを改善してデプロイ時間を 1/4 にした話 | How I improved the build of my Scala application and reduced deployment time by 4x
nomadblacky
1
180
API Platform for Laravel
dunglas
0
320
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.3k
RailsConf 2023
tenderlove
28
810
Optimising Largest Contentful Paint
csswizardry
31
2.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
157
15k
Designing for humans not robots
tammielis
248
25k
VelocityConf: Rendering Performance Case Studies
addyosmani
322
23k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
278
13k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
19k
A better future with KSS
kneath
235
17k
GraphQLとの向き合い方2022年版
quramy
43
13k
Music & Morning Musume
bryan
46
6k
Unsuck your backbone
ammeep
667
57k
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文走るよ ね、ってことを思った。