Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### The controller
- def index
- @products = Product.all(:order => [:model_number, :model_name])
- display @products
- end
- ### The model
- belongs_to :manufacturer
- belongs_to :category
- belongs_to :group
- belongs_to :gsa_contract
- belongs_to :geographic_origin
- belongs_to :sin_number
- has n, :photos
- ### The view
- %table#products.products{ :cellpadding => 3, :cellspacing=>0 }
- %thead
- %tr
- - ['Image', 'Model Number', 'Name', 'Price', 'Cur', "Man", "Group", "Geo"].each do |table_header|
- %th= table_header
- %tbody
- - @products.each do |product|
- %tr[product]
- %td.image
- %img{ :src=> product.photo.url(:tiny), :width => 30, :height => 30 }
- %td.model_number= product.model_number #, url(:product, :id => product.id)
- %td.model_name= product.model_name
- %td.price_range= product.price_range
- %td.currency= product.currency
- %td.manufacturer= product.manufacturer ? product.manufacturer.title : "None"
- %td.group= product.group ? product.group.title : "None"
- %td.geo= product.geographic_origin ? product.geographic_origin.title : "None"
- ### Explanation
- #
- # What happens is this (shortened to make it easier to read):
- #
- # ~ SELECT `id`, `model_number`, `model_name`, ... FROM `products` ORDER BY `model_number`, `model_name`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (64, 721, ...)
- # ~ 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`
- # ~ SELECT `id`, `title`, `created_at`, `updated_at` FROM `groups` WHERE (`id` IN (3, 110, ...)) ORDER BY `id`
- # ~ SELECT `id`, `title`, `created_at`, `updated_at` FROM `geographic_origins` WHERE (`id` IN (1, 4, ...)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (157)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (176)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (172)) ORDER BY `id`
- # ~ SELECT `id`, `filename`, `content_type`, `file_size`, `drawing`, `thumbnailed`, `original_url`, `product_id` FROM `photos` WHERE (`product_id` IN (325)) ORDER BY `id`
- # ... (and so on)
- #
- # 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.
- #
- # 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