Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. ### The controller
  2.  
  3. def index
  4. @products = Product.all(:order => [:model_number, :model_name])
  5. display @products
  6. end
  7.  
  8. ### The model
  9.  
  10. belongs_to :manufacturer
  11. belongs_to :category
  12. belongs_to :group
  13. belongs_to :gsa_contract
  14. belongs_to :geographic_origin
  15. belongs_to :sin_number
  16.  
  17. has n, :photos
  18.  
  19. ### The view
  20.  
  21. %table#products.products{ :cellpadding => 3, :cellspacing=>0 }
  22. %thead
  23. %tr
  24. - ['Image', 'Model Number', 'Name', 'Price', 'Cur', "Man", "Group", "Geo"].each do |table_header|
  25. %th= table_header
  26. %tbody
  27. - @products.each do |product|
  28. %tr[product]
  29. %td.image
  30. %img{ :src=> product.photo.url(:tiny), :width => 30, :height => 30 }
  31. %td.model_number= product.model_number #, url(:product, :id => product.id)
  32. %td.model_name= product.model_name
  33. %td.price_range= product.price_range
  34. %td.currency= product.currency
  35. %td.manufacturer= product.manufacturer ? product.manufacturer.title : "None"
  36. %td.group= product.group ? product.group.title : "None"
  37. %td.geo= product.geographic_origin ? product.geographic_origin.title : "None"
  38.  
  39. ### Explanation
  40. #
  41. # What happens is this (shortened to make it easier to read):
  42. #
  43. # ~ SELECT `id`, `model_number`, `model_name`, ... FROM `products` ORDER BY `model_number`, `model_name`
  44. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (64, 721, ...)
  45. # ~ SELECT `id`, `title`, `gsa_discount`, `gsa_markup`, `net_discount`, `ecommerce_discount`, `created_at`, `updated_at` FROM `manufacturers` WHERE (`id` IN (1, 2, 6, 10, 13, 11, 5, 12)) ORDER BY `id`
  46. # ~ SELECT `id`, `title`, `created_at`, `updated_at` FROM `groups` WHERE (`id` IN (3, 110, ...)) ORDER BY `id`
  47. # ~ SELECT `id`, `title`, `created_at`, `updated_at` FROM `geographic_origins` WHERE (`id` IN (1, 4, ...)) ORDER BY `id`
  48. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
  49. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (157)) ORDER BY `id`
  50. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
  51. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (176)) ORDER BY `id`
  52. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
  53. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (172)) ORDER BY `id`
  54. # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
  55. # ... (and so on)
  56. #
  57. # You can see it's loading each image row again, instead of using what it got in the huge load it did with all the id's. What is really strange is that 325 loads every other time seperately.
  58. #
  59. # This is actually looping over aroun 753 records, but it only loads 47 individual images (which includes the 325 every other time).
Add Comment
Please, Sign In to add comment