Select custom MySQL fields with inclusion

Nauhaie None wrote:

Hi all,

I have a big problem. I am displaying a list of articles which displays the title of the article and the author's name.

The problem is that :select option seems to be incompatible with :include. When I do not include author

Article.find(:all, :select=>"articles.title, articles.id, articles.author_id"), it only selects the title as expected, but it has to do an additional query for every author.

However, if I run

Article.find(:all, :select=>"articles.title, articles.id, articles.author_id", :include=>:author)

It still selects every field from articles and authors... Why?

Use of eager loading causes a special select to be used so
that the joined table can be separated into a hierarchy
of models.

Is efficiency the only reason you want to limit your select set?
If so, you'd have to write this to pull the author name into the
fetched article models:

Article.find( :all,
   :select => 'articles.id, articles.title, authors.name as author_name',
   :joins => 'left join authors on authors.id = articles.author_id' )