Upcoming SlideShare
×

# ActiveRecord is Rotting Your Brian

1,221 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,221
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
9
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.
17. 17. class Order < ActiveRecord::Base belongs_to :user delegate :street, :to => :userendclass User < ActiveRecord::Base has_one :address delegate :street, :to => :addressend
19. 19. N+1users = User.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])