-
Notifications
You must be signed in to change notification settings - Fork 57
Conversation
# would cause errors when rendering | ||
def fix_cart_items | ||
session[:cart].items.each do |em, _count| | ||
session[:cart].items.delete(em) unless EquipmentModel.find_by_id(em) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this queries the db for every item in the cart. I guess you'd only really notice if you had lots of items in it, but it's expensive for an edge case handler. EquipmentModel.where(id: items.keys).count == items.count
uses 1 query to see if the # of records returned by looking at the keys matches the original # of keys so you could guard against having to actually do the iteration unless it was necessary.
Actually, then you could just use the returned records to cherry pick the items hash using select
valid_items = EquipmentModel.where(id: items.keys).collect(&:id)
items = items.select{ |em, _count| valid_items.include? em }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hot damn that's really nice. Look at you w/ your fancy db query optimization :-P.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
honestly i finally got comfortable with collect, inject and select while doing the reports and they are the most amazing things
Goddamn spelling :-P. This should now be much leaner and I moved the |
Ok, now we're ready. |
awesome, squash/merge away |
better method with fewer db queries specs ftw
Woot passing tests! Merging! |
Resolves #1085