public
Description: Allows the use of :select with joins based includes
Homepage:
Clone URL: git://github.com/fcheung/selectable_includes.git
fcheung (author)
Sat Dec 20 14:50:10 -0800 2008
commit  d344f9d2a83d5a04a9de8f093f3fff81b2c7210b
tree    658f5e7a9b3a37a7111666e3ea79ff41a0879d52
parent  8f0ba667f637dce6fb4d989b59f04598a50c93e0
name age message
file README.markdown Sat Dec 20 14:50:10 -0800 2008 renamed readme [fcheung]
file Rakefile Sat Dec 20 14:49:10 -0800 2008 First release [fcheung]
file init.rb Sat Dec 20 14:49:10 -0800 2008 First release [fcheung]
directory lib/ Sat Dec 20 14:49:10 -0800 2008 First release [fcheung]
directory test/ Sat Dec 20 14:49:10 -0800 2008 First release [fcheung]
README.markdown

Selectable Includes

Active Record has two strategies for :include. In one each association is loaded with a query and in the other a single query with lots of tasty joins loads all of the associations. In this latter form Active Record ignores any :select option you pass because it needs to control how all the retrieved columns are aliased in order to put all the records together.

This plugin allows the :select option to be taken into account in such circumstances. Unlike a normal :select option it does not replace the select statement generated by Active Record (since would break the eager loading) but is prepended to it. What this means is that you cannot control which columns are selected, but you can piggy back attributes.

For example

Post.find :all, :include => :comments, :order => 'comments.created_at desc', :select => "posts.title"

accomplishes nothing

whereas

`Post.find :all, :include => :comments, :order => 'comments.created_at desc',

     :select => "(SELECT COUNT(*) from favourite_posts where posts.id = favourite_posts.post_id) as favourite_count"`

would piggy back the number of times the post was added to someone's favourite to the list of fetched posts.

You could also use this if you wanted a query that looked like

SELECT SQL_NO_CACHE ...

you could do

Post.find :all, :include => :comments, :order => 'comments.created_at desc', :select => "SQL_NO_CACHE"

This plugin has no effect in cases where the joins base include code is not used.