ActiveRecord is Rotting Your Brian
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

ActiveRecord is Rotting Your Brian

on

  • 1,218 views

 

Statistics

Views

Total Views
1,218
Views on SlideShare
1,216
Embed Views
2

Actions

Likes
0
Downloads
7
Comments
0

2 Embeds 2

https://twitter.com 1
http://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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
  • 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
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

ActiveRecord is Rotting Your Brian Presentation Transcript

  • 1. ActiveRecord is Rotting Your Brain@ethangunderson Great Lakes Ruby Bash 2011
  • 2. Order.all.select{ |order| order.status == ‘open’ } Order.all.slice(0, N).map(&:id)
  • 3. Abstractions
  • 4. “All non-trivialabstractions, tosome degree, areleaky.” - Joel Spolsky
  • 5. The Law of Leaky Abstractions
  • 6. Order.all.select{ |order| order.status == ‘open’ }
  • 7. SELECT * FROM orders;Typecast each record returned into an ActiveRecord Order object.
  • 8. Order.all.select{ |order| order.status == ‘open’ }
  • 9. class Order < ActiveRecord::Base named_scope :open, where(:status => ‘open’)endOrders.openOrder.where(:status => ‘open’)
  • 10. SELECT * FROM orders where status = ‘open’;
  • 11. validates_uniqueness_of
  • 12. User 1: Checksfor uniqueness User 2: Checks for uniquenessUser 1: Inserts record User 2: Inserts record
  • 13. SolutionCreate a Unique Index
  • 14. Law of Demeter
  • 15. Each unit should have only limited knowledge about other units: onlyunits "closely" related to the current unit. Each unit should only talk to its friends; dont talk to strangers.Only talk to your immediate friends.
  • 16. order.user.address.stree t
  • 17. class Order < ActiveRecord::Base belongs_to :user delegate :street, :to => :userendclass User < ActiveRecord::Base has_one :address delegate :street, :to => :addressend
  • 18. class Order < ActiveRecord::Base belongs_to :user def address_street user.address_street endendclass User < ActiveRecord::Base has_one :address def address_street address.street endend
  • 19. N+1users = User.allusers.each do |user| user.address.streetend
  • 20. 1users = User.includes(:address).allusers.each do |user| user.address.streetend
  • 21. N*OMGorders = Order.allorders.each do |order| order.item.name order.user.address.street order.item.merchant.name *end
  • 22. orders = Order.includes(:user=> :address).allorders.each do |order| order.item.name order.user.address.street order.item.merchant.name *end
  • 23. ActiveRecordAll Over The Place
  • 24. Connascence
  • 25. Connascence of NameConnascence of TypeConnascence of MeaningConnascence of PositionConnascence ofAlgorithmConnascence ofExecutionConnascence of TimingConnascence of Identity
  • 26. Rule of DegreeConnascence of NameConnascence of TypeConnascence of MeaningConnascence of PositionConnascence of AlgorithmConnascence of ExecutionConnascence of TimingConnascence of Identity
  • 27. order.update_attributes({ :status => ‘closed’ })
  • 28. order.close!
  • 29. Connascence of MeaningConnascence of Name
  • 30. user = User.find(params[:user_id])user.orders.create(params[:order])
  • 31. user = User.find(params[:user_id])user.add_order(params[:order])
  • 32. current_user.orders.open.order(:created_at)
  • 33. current_user.current_orders
  • 34. The Database is Your Friend
  • 35. github.com/trptcolin/consistency_fail
  • 36. In your ~/.irbrc...if Object.const_defined?(ActiveRecord) ActiveRecord::Base.logger = Logger.new(STDOUT)end
  • 37. https://github.com/dsboulder/ query_reviewer
  • 38. Thanks!
  • 39. Hiring