Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ActiveRecord Anti-Patterns
Search
Ethan Gunderson
November 04, 2011
Technology
320
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ActiveRecord Anti-Patterns
Ethan Gunderson
November 04, 2011
Other Decks in Technology
See All in Technology
SREの視点で考えるSIEM活用術 〜AWS環境でのセキュリティ強化〜
cscengineer
PRO
0
110
AIで社員の自主発信に広報目線を組み込む
_mossann_t
0
130
Vibe Coding で作ったプロダクトをどう安全に動かすか / How to Safely Run Products Built with Vibe Coding
glidenote
0
170
AIに任せた品質は、誰が見立てるのか - AI時代のテストマネジメント
nakanao
3
2.2k
Goodbye ShellScript, Hello File-based App
shunsock
0
260
AIによるクリエイティブ生成を行う上での試行錯誤
plaidtech
PRO
0
160
aws-iot-platform-architecture-use-cases.pdf
ma2shita
0
320
10Xに技術的負債をもたらした「2つの境界の歪み」その構造と解消への営み
10xinc
0
2.1k
EventBridge に「合流」はない ― サーバーレスのワークフローを育てるということ / No Join in EventBridge
yusukeshimizu
2
160
え、こんなに早く改修できるの?──新人エンジニアとスクラムマスターの2人が語る、AI×アジャイル開発の現場
ysasago
2
460
作って終わりじゃないサーバーレス 〜9年運用する大規模EC物流API基盤の設計・運用のリアル〜
zozotech
PRO
0
170
バイブコーディング時代のWebアプリ開発入門~Cloud Runで学ぶセキュアなビルドとデプロイ
waiwai2111
1
140
Featured
See All Featured
Abbi's Birthday
coloredviolet
4
10k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
Technical Leadership for Architectural Decision Making
baasie
3
570
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
360
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
510
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
300
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Producing Creativity
orderedlist
PRO
348
41k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
330
Building Flexible Design Systems
yeseniaperezcruz
330
41k
How to make the Groovebox
asonas
2
2.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Transcript
Anti-Patterns for fun and profit Active Record Friday, November 4,
2011
Ethan Gunderson @ethangunderson Friday, November 4, 2011
Friday, November 4, 2011
20% 80% Friday, November 4, 2011
Order.all.slice(0, N).select{ |order| order.status == ‘open’} Friday, November 4, 2011
Common Mistakes Friday, November 4, 2011
ActiveRecord Safari Friday, November 4, 2011
“All non-trivial abstractions, to some degree, are leaky.” - Joel
Spolsky Friday, November 4, 2011
Order.all.slice(0, N).select{ |order| order.status == ‘open’} Friday, November 4, 2011
class Order < ActiveRecord::Base scope :open, where(:status => ‘open’) end
Order.open.limit(5) Friday, November 4, 2011
change_table(:users) do |table| table.string :email table.string :status end Migrations Friday,
November 4, 2011
1. Write lock the table. 2. Make a copy of
the table. 3. Make changes to the copy. 4. Promote copy to primary. 5. Repeat 1-4. Friday, November 4, 2011
connection.execute(“ALTER TABLE `users` ADD COLUMN email varchar(255) DEFAULT NULL, ADD
COLUMN status varchar(10) DEFAULT NULL”) Friday, November 4, 2011
change_table(:users, :bulk => true) do |table| table.string :email table.string :status
end Friday, November 4, 2011
The Leaks, they add up. Friday, November 4, 2011
Not Optimizing Queries Friday, November 4, 2011
Order.all.each do |order| #something end Friday, November 4, 2011
Order.all.each do |order| #something end Ouch Friday, November 4, 2011
Iterate over the DB cursor and construct an array of
rows. Initiate an array of ActiveRecord objects. Friday, November 4, 2011
Order.find_each do |order| #something end Friday, November 4, 2011
Order.find_in_batches do |orders| #something end Friday, November 4, 2011
N + 1 Order.all.each do |order| order.user.first_name end Friday, November
4, 2011
N*OMG Order.all.each do |order| order.user.first_name order.user.address.street ... end Friday, November
4, 2011
1 Order.all.includes(:user).each do |order| order.user.first_name end Friday, November 4, 2011
Order.all.includes(:user => :address) each do |order| order.user.first_name order.user.address.street ... end
Friday, November 4, 2011
https://github.com/nesquena/ query_reviewer Friday, November 4, 2011
if Object.const_defined?('ActiveRecord') ActiveRecord::Base.logger = Logger.new (STDOUT) end In your ~/.irbrc...
Friday, November 4, 2011
Race Conditions Friday, November 4, 2011
validates_uniqueness_of Friday, November 4, 2011
Works. Until you hit production. Friday, November 4, 2011
User 1 Checks for uniqueness User 2 Friday, November 4,
2011
User 1 Checks for uniqueness User 2 Checks for uniqueness
Friday, November 4, 2011
User 1 Inserts record User 2 Inserts record Checks for
uniqueness Checks for uniqueness Friday, November 4, 2011
Success! Friday, November 4, 2011
Success! Danger! Friday, November 4, 2011
create_index :users, :email, :unique => true Friday, November 4, 2011
github.com/trptcolin/consistency_fail Friday, November 4, 2011
Abusive Callbacks Friday, November 4, 2011
Loading Associations Friday, November 4, 2011
class Order < ActiveRecord::Base validate :validate_cc_record def validate_cc_record cc.user !=
user end end Friday, November 4, 2011
Take advantage of dirty attributes Friday, November 4, 2011
class Order < ActiveRecord::Base validate :validate_cc_record def validate_cc_record return unless
cc_id.changed? cc.user != user end end Friday, November 4, 2011
class Order < ActiveRecord::Base before_save :update_item_count def update_sold_count item.increment_sold_count(1) end
end Friday, November 4, 2011
This is slow Friday, November 4, 2011
This is *really* hard to debug in a large application.
Friday, November 4, 2011
Order Item Vendor Sales Friday, November 4, 2011
Order Item Vendor Sales Save fails, returns false Friday, November
4, 2011
Order Item Vendor Sales Save fails, returns false Friday, November
4, 2011
Keep callbacks simple. Friday, November 4, 2011
Breaking the Law Friday, November 4, 2011
Demeter Friday, November 4, 2011
Each unit should have only limited knowledge about other units:
only units "closely" related to the current unit. Each unit should only talk to its friends; don't talk to strangers. Only talk to your immediate friends. Friday, November 4, 2011
You can play with yourself. You can play with your
toys. You can play with toys given to you. You can play with toys you’ve made yourself. http://c2.com/cgi/wiki?LawOfDemeter Friday, November 4, 2011
order.user.address Friday, November 4, 2011
But why do I care? Friday, November 4, 2011
Coupling Friday, November 4, 2011
0 25 50 75 100 Coupling Cost of Change Friday,
November 4, 2011
order.user.address Friday, November 4, 2011
class Order < ActiveRecord::Base delegate :address, :to => :user, :prefix
=> true, :allow_nil => true end Friday, November 4, 2011
order.user_address Friday, November 4, 2011
class Order < ActiveRecord::Base def user_address #different implementation end end
Friday, November 4, 2011
Recap Friday, November 4, 2011
Pay attention to the abstraction Friday, November 4, 2011
Optimize those queries Friday, November 4, 2011
Avoid complicated callbacks Friday, November 4, 2011
Itʼs the law. Follow it. Friday, November 4, 2011
The database is your friend. Friday, November 4, 2011
YMMV Friday, November 4, 2011
Thanks! Friday, November 4, 2011