Can anyone point me to a resource for hand coding some sql so I can create my own for the following models: I''m using single table inheritance to store related objects eg: Thumbnail < Image < File :: ActiveRecord:Base Image has_many thumbnails Thumbnail has_one image When I come to list thumbnails I''d like to be able to: 1. List all images relating to a product 2. For each Image get a link to the smallest thumbnail 64px x 64px and link to it. 3. List all thumbnails related to an image I figure I need to 3 things: 1. Select images related to a product 2. JOIN my each image to it''s related small thumbnail giving me something like: image.thumbnail_url 3. JOIN all thumbnails to their related images so I can use @image.thumbnails Other wise I''m running N+1 queries to try and achieve a similar result. I''ll take any advice, even if it''s that I''m going about this completely the wrong way! Cheers, Graham
Francois Beausoleil
2005-Oct-18 17:19 UTC
Re: hand writing sql for eager loading of thumbnails
Hi ! 2005/10/18, Graham <g.arrowsmith@gmail.com>:> Can anyone point me to a resource for hand coding some sql so I can create my > own for the following models:Have you investigated the :include option of find ? Basically, when you find an object, you can preload selected relationships. You'd use it like this: images = Image.find(:all, :conditions => [...], :include => :thumbnail) images.each do |img| img.thumbnail.url end Hope that helps ! François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Graham Arrowsmith
2005-Oct-18 17:52 UTC
Re: hand writing sql for eager loading of thumbnails
Francois Beausoleil <francois.beausoleil@...> writes:> > images = Image.find(:all, :conditions => [...], :include => :thumbnail) > images.each do |img| > img.thumbnail.url > end > > Hope that helps ! > François >Hi François, I''m afraid the :include won''t work as :thumbnail and :image share the same table (single table inheritance) - as they''re identical and made sense to use one table. DHH made reference to this in a previous post: It’s currently not possible to use eager loading on multiple associations from the same table. http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html So I''m trying to write the sql myself. (Trying as opposed to succeeding...)
Jeremy Evans
2005-Oct-18 18:53 UTC
Re: Re: hand writing sql for eager loading of thumbnails
> DHH made reference to this in a previous post: > It''s currently not possible to use eager loading on multiple associations > from the same table. > http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html > > So I''m trying to write the sql myself. (Trying as opposed to succeeding...)You could try the current patch that does this, which is in ticket 2172. It was marked for inclusion for 1.0 until this past weekend. Unfortunately, it was decided that the patch wasn''t 1.0 ready. No reason was given, but my guess is that this is because the patch is large, affects all the database adapters, and affects numerous other areas in addition to multiple associations on the same table. The earlier patch (in ticket 1562) that just implements support for multiple associations from the same table was closed as wontfix because it was combined into a later patch in 2172. Instead of just applying the patch in 1562 (and waiting until later to apply the other improvements in 2172), the decision was made to release 1.0 without the functionality. Disclaimer: I wrote most of the code to support multiple associations on the same table, and it''s certainly possible that it was that code that wasn''t ready for 1.0 (again, no reason was given, so I don''t know).