ActiveRecord is Rotting Your Brian

1,221 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,221
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \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

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

    ×