This really is much more of a "so why do things work by doing this" question as opposed to a "I'm not sure how to get this doneInch question...

Therefore the gospel on tugging connected records you know you are likely to use is by using :include because you will get a join and steer clear of a lot of extra queries:

Publish.all(:include => :comments)

But if you consider the logs, there is no join happening:

Publish Load (3.7ms)   Choose * FROM "posts"

Comment Load (.2ms)   Choose "comments.*" FROM "comments"

                       WHERE ("comments".publish_id IN (1,2,3,4))

                       ORDER BY produced_at asc)

It is going for a shortcut since it pulls all the comments at the same time, but it is still not really a join (that is what all of the documentation appears to express). The only method I'm able to obtain a join is by using :joins rather than :include:

Publish.all(:joins => :comments)

And also the logs show:

Publish Load (6.0ms)  Choose "posts".* FROM "posts"

                   INNER JOIN "comments" ON "posts".id = "comments".publish_id

Am I missing something? I've an application with six associations as well as on one screen I display data from these. Appears like it might be easier to have one join-erectile dysfunction query rather than 6 people. I understand that performance-smart it isn't always easier to perform a join instead of individual queries (actually if you are going through time spent, it appears such as the two individual queries above are faster compared to join), but in the end the paperwork I have been reading through I am surprised to determine :include no longer working as marketed.

Maybe Rails is aware from the performance problem and does not join except in some cases?

It seems the :include functionality was transformed with Rails 2.1. Rails i did so the participate in every case, however for performance reasons it had been transformed to make use of multiple queries in certain conditions. This web site publish by Fabio Akita has good quality info on the modification (begin to see the section titled "Enhanced Eager Loading").

The main difference between joins and can include is the fact that while using include statement creates a significantly bigger SQL query loading into memory all of the characteristics in the other table(s).

For instance, for those who have a table filled with comments and also you make use of a :joins => customers to drag in most the consumer information for sorting reasons, etc it'll work fine and take a shorter period than :include, but say you need to display the comment together with the customers title, email, etc. To find the information using :joins, it'll have to create separate SQL queries for every user it brings, whereas should you used :include this post is ready to be used.

Great example:

Additionally to some performance factors, there is a functional difference too. Whenever you join comments, you're requesting posts which have comments- an inner join automatically. Whenever you include comments, you're requesting all posts- an outer join.