SlideShare una empresa de Scribd logo
1 de 39
ActiveRecord
         is Rotting Your Brain




@ethangunderson
  Great Lakes Ruby Bash 2011
Order.all.select{ |order| order.status == ‘open’ }


         Order.all.slice(0, N).map(&:id)
Abstractions
“All non-trivial
abstractions, to
some degree, are
leaky.”
         - Joel Spolsky
The Law of Leaky
  Abstractions
Order.all.select{ |order| order.status == ‘open’ }
SELECT * FROM orders;

Typecast each record returned
 into an ActiveRecord Order
           object.
Order.all.select{ |order| order.status == ‘open’ }
class Order < ActiveRecord::Base
 named_scope :open,
   where(:status => ‘open’)
end

Orders.open


Order.where(:status => ‘open’)
SELECT * FROM orders where status =
             ‘open’;
validates_uniqueness_of
User 1: Checks
for uniqueness
                     User 2: Checks
                     for uniqueness
User 1: Inserts
   record
                  User 2: Inserts record
Solution
Create a Unique Index
Law of Demeter
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.
order.user.address.stree
            t
class Order < ActiveRecord::Base
 belongs_to :user
 delegate :street, :to => :user
end


class User < ActiveRecord::Base
 has_one :address
 delegate :street, :to => :address
end
class Order < ActiveRecord::Base
 belongs_to :user

 def address_street
  user.address_street
 end
end

class User < ActiveRecord::Base
 has_one :address

 def address_street
  address.street
 end
end
N+1
users = User.all
users.each do |user|
 user.address.street
end
1
users = User.includes(:address).all
users.each do |user|
 user.address.street
end
N*OMG
orders = Order.all
orders.each do |order|
 order.item.name
 order.user.address.street
 order.item.merchant.name
 *
end
orders = Order.includes(:user
=> :address).all
orders.each do |order|
 order.item.name
 order.user.address.street
 order.item.merchant.name
 *
end
ActiveRecord
All Over The Place
Connascence
Connascence of Name
Connascence of Type
Connascence of Meaning
Connascence of Position
Connascence of
Algorithm
Connascence of
Execution
Connascence of Timing
Connascence of Identity
Rule of Degree
Connascence of Name
Connascence of Type
Connascence of Meaning
Connascence of Position
Connascence of Algorithm
Connascence of Execution
Connascence of Timing
Connascence of Identity
order.update_attributes({
 :status => ‘closed’
 })
order.close!
Connascence of
     Meaning


Connascence of Name
user = User.find(params[:user_id])
user.orders.create(params[:order])
user = User.find(params[:user_id])
user.add_order(params[:order])
current_user.orders.open.order(:created_a
t)
current_user.current_orders
The Database
       is
   Your Friend
github.com/trptcolin/consistency_fail
In your ~/.irbrc...

if Object.const_defined?('ActiveRecord')
  ActiveRecord::Base.logger =
  Logger.new(STDOUT)
end
https://github.com/dsboulder/
        query_reviewer
Thanks!
Hiring

Más contenido relacionado

La actualidad más candente

Reactor Design Pattern
Reactor Design PatternReactor Design Pattern
Reactor Design Patternliminescence
 
Introduction to Unit Testing (Part 1 of 2)
Introduction to Unit Testing (Part 1 of 2)Introduction to Unit Testing (Part 1 of 2)
Introduction to Unit Testing (Part 1 of 2)Dennis Byrne
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerDarwin Durand
 
Χρήση Vba για την πρόσβαση σε βάση δεδομένων
Χρήση Vba για την πρόσβαση σε βάση δεδομένωνΧρήση Vba για την πρόσβαση σε βάση δεδομένων
Χρήση Vba για την πρόσβαση σε βάση δεδομένωνNikos Mpalatsoukas
 
Stay with React.js in 2020
Stay with React.js in 2020Stay with React.js in 2020
Stay with React.js in 2020Jerry Liao
 
Evernote ウェブサービスAPI
Evernote ウェブサービスAPIEvernote ウェブサービスAPI
Evernote ウェブサービスAPIFumihiro Kato
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventasDAYANA RETO
 
The Ring programming language version 1.5.1 book - Part 37 of 180
The Ring programming language version 1.5.1 book - Part 37 of 180The Ring programming language version 1.5.1 book - Part 37 of 180
The Ring programming language version 1.5.1 book - Part 37 of 180Mahmoud Samir Fayed
 
Unit Test and TDD
Unit Test and TDDUnit Test and TDD
Unit Test and TDDViet Tran
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012Sandeep Joshi
 
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...Mail.ru Group
 

La actualidad más candente (16)

Specs2
Specs2Specs2
Specs2
 
Reactor Design Pattern
Reactor Design PatternReactor Design Pattern
Reactor Design Pattern
 
Introduction to Unit Testing (Part 1 of 2)
Introduction to Unit Testing (Part 1 of 2)Introduction to Unit Testing (Part 1 of 2)
Introduction to Unit Testing (Part 1 of 2)
 
mediator
mediatormediator
mediator
 
Visual Studio.Net - Sql Server
Visual Studio.Net - Sql ServerVisual Studio.Net - Sql Server
Visual Studio.Net - Sql Server
 
Χρήση Vba για την πρόσβαση σε βάση δεδομένων
Χρήση Vba για την πρόσβαση σε βάση δεδομένωνΧρήση Vba για την πρόσβαση σε βάση δεδομένων
Χρήση Vba για την πρόσβαση σε βάση δεδομένων
 
React hooks
React hooksReact hooks
React hooks
 
Stay with React.js in 2020
Stay with React.js in 2020Stay with React.js in 2020
Stay with React.js in 2020
 
Evernote ウェブサービスAPI
Evernote ウェブサービスAPIEvernote ウェブサービスAPI
Evernote ウェブサービスAPI
 
Kode vb.net
Kode vb.netKode vb.net
Kode vb.net
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
 
The Ring programming language version 1.5.1 book - Part 37 of 180
The Ring programming language version 1.5.1 book - Part 37 of 180The Ring programming language version 1.5.1 book - Part 37 of 180
The Ring programming language version 1.5.1 book - Part 37 of 180
 
Vs c# lecture11
Vs c# lecture11Vs c# lecture11
Vs c# lecture11
 
Unit Test and TDD
Unit Test and TDDUnit Test and TDD
Unit Test and TDD
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
 
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
CocoaHeads Moscow. Азиз Латыпов, VIPole. «Запросы в CoreData с агрегатными фу...
 

Destacado

Destacado (6)

The ABCs of Scaling MongoDB
The ABCs of Scaling MongoDBThe ABCs of Scaling MongoDB
The ABCs of Scaling MongoDB
 
#IAEM November 2010
#IAEM November 2010#IAEM November 2010
#IAEM November 2010
 
Haml and Sass Introduction
Haml and Sass IntroductionHaml and Sass Introduction
Haml and Sass Introduction
 
Mongo chicago
Mongo chicagoMongo chicago
Mongo chicago
 
Our Adventure with MongoDB
Our Adventure with MongoDBOur Adventure with MongoDB
Our Adventure with MongoDB
 
On the Papers of Giants
On the Papers of GiantsOn the Papers of Giants
On the Papers of Giants
 

Similar a ActiveRecord is Rotting Your Brian

Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2RORLAB
 
Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1RORLAB
 
ActiveRecord Query Interface (1), Season 1
ActiveRecord Query Interface (1), Season 1ActiveRecord Query Interface (1), Season 1
ActiveRecord Query Interface (1), Season 1RORLAB
 
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...MongoDB
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)tarcieri
 
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2RORLAB
 

Similar a ActiveRecord is Rotting Your Brian (6)

Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2Active Record Query Interface (1), Season 2
Active Record Query Interface (1), Season 2
 
Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1Active Record Query Interface (2), Season 1
Active Record Query Interface (2), Season 1
 
ActiveRecord Query Interface (1), Season 1
ActiveRecord Query Interface (1), Season 1ActiveRecord Query Interface (1), Season 1
ActiveRecord Query Interface (1), Season 1
 
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
MongoDB World 2019: MongoDB Read Isolation: Making Your Reads Clean, Committe...
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)
 
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2
 

Último

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Último (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

ActiveRecord is Rotting Your Brian

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. Connascence of Name\nConnascence of name is when multiple components must agree on the name of an entity.\nConnascence of Type\nConnascence of type is when multiple components must agree on the type of an entity.\nConnascence of Meaning\nConnascence of meaning is when multiple components must agree on the meaning particular values.\nConnascence of Position\nConnascence of positions is when multiple components must agree on the order of values.\nConnascence of Algorithm\nConnascence of algorithm is when multiple components must agree on a particular algorithm.\nConnascence of Execution (order)\nConnascence of execution is when the order of execution of multiple components is important.\nConnascence of Timing\nConnascence of timing is when the timing of the execution of multiple components is important.\nConnascence of Identity\nConnascence of identity is when multiple components must reference the entity.\n\n
  27. Connascence of Name\nConnascence of name is when multiple components must agree on the name of an entity.\nConnascence of Type\nConnascence of type is when multiple components must agree on the type of an entity.\nConnascence of Meaning\nConnascence of meaning is when multiple components must agree on the meaning particular values.\nConnascence of Position\nConnascence of positions is when multiple components must agree on the order of values.\nConnascence of Algorithm\nConnascence of algorithm is when multiple components must agree on a particular algorithm.\nConnascence of Execution (order)\nConnascence of execution is when the order of execution of multiple components is important.\nConnascence of Timing\nConnascence of timing is when the timing of the execution of multiple components is important.\nConnascence of Identity\nConnascence of identity is when multiple components must reference the entity.\n\n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n