Do these two statements in The Rails 5 Way contradict each other?











up vote
2
down vote

favorite












I'm reading "The Rails 5 Way" now, and I've reached the section covering includes and joins. Up until now my understanding has been that includes is best-suited for eager-loading DB data into memory in order to avoid N+1 queries, while joins is more appropriate for doing simple filtering of a query result set based on associated tables, without actually accessing the attributes on those associated table rows:



1




includes(*associations)
Active Record has the capability to eliminate
“N+1” queries by letting you specify what associations to eager load
using the includes method or option in your finders. Active Record
will load those relationships with the minimum number of queries
possible.




2




joins(expression) joins works similarly to includes using an INNER
JOIN in the resulting SQL query. One of the key bits of knowledge to
understand about inner joins is they return only the set of records
that match the tables being joined. If a row on either side of the
join is missing its corresponding row on the other side, neither will
be returned in the result set.




However, on page 202 I see the following:



3




By far the most common usage of the joins method is to eager-fetch
data for associated objects in a single SELECT statement in order to
prevent so-called N+1 queries.




The usage described in quote #3 seems to be a closer fit with quote #1 than quote #2, i.e. it talks about the goal of reducing N+1 queries by eager-loading the data (which I know includes does by calling either eager_load or preload under the hood). To be honest, quote #3 doesn't appear to match #1 at all.



My question is- how can we reconcile these 3 quotes with each other? Or are they already in sync with each other and I'm simply missing something?










share|improve this question




















  • 4




    Third quote is simply incorrect. Joins does not load associated records.
    – Marcin Kołodziej
    Nov 10 at 23:19










  • Eager vs lazy is orthogonal to includes vs join. Includes implicitly joins. If you do N+1 instead of avoiding it then obviously specify N query is "eager" in the informal sense that you evaluate it before the +1 query. The use of "eager" in quote 3 is wrong--they mean something like, simultaneously specify the N & (via the join) the +1. PS Please give a link.
    – philipxy
    Nov 10 at 23:21












  • i think we're confusing with eager-load vs eager-fetch in third quote. i'm sure the writer would have used eager-load as that term is more common; There has to be a reason why eager-fetch is used. Using joins, yes I do see how data for associated objects can be eager fetched. The sentence actually does say "how data for associated objects"; Building the associated objects through joins was probably out of scope for this quote.
    – vee
    Nov 11 at 3:29












  • Could be that the author is talking about selecting from the joined table without loading the association. Foo.select(“foos.*, bars.baz AS baz”).joins(:bar). This can be used with aggregates like count or average. But this is not actually that common.
    – max
    Nov 11 at 9:48












  • @philipxy- can you clarify what kind of link you mean? The 3 quotes are from a book, not from a page I can link to. I could link to the book's page on Amazon, if that will help, but I did mention the book's title already so I'm not sure how much added benefit that would be.
    – Richie Thomas
    Nov 11 at 18:27

















up vote
2
down vote

favorite












I'm reading "The Rails 5 Way" now, and I've reached the section covering includes and joins. Up until now my understanding has been that includes is best-suited for eager-loading DB data into memory in order to avoid N+1 queries, while joins is more appropriate for doing simple filtering of a query result set based on associated tables, without actually accessing the attributes on those associated table rows:



1




includes(*associations)
Active Record has the capability to eliminate
“N+1” queries by letting you specify what associations to eager load
using the includes method or option in your finders. Active Record
will load those relationships with the minimum number of queries
possible.




2




joins(expression) joins works similarly to includes using an INNER
JOIN in the resulting SQL query. One of the key bits of knowledge to
understand about inner joins is they return only the set of records
that match the tables being joined. If a row on either side of the
join is missing its corresponding row on the other side, neither will
be returned in the result set.




However, on page 202 I see the following:



3




By far the most common usage of the joins method is to eager-fetch
data for associated objects in a single SELECT statement in order to
prevent so-called N+1 queries.




The usage described in quote #3 seems to be a closer fit with quote #1 than quote #2, i.e. it talks about the goal of reducing N+1 queries by eager-loading the data (which I know includes does by calling either eager_load or preload under the hood). To be honest, quote #3 doesn't appear to match #1 at all.



My question is- how can we reconcile these 3 quotes with each other? Or are they already in sync with each other and I'm simply missing something?










share|improve this question




















  • 4




    Third quote is simply incorrect. Joins does not load associated records.
    – Marcin Kołodziej
    Nov 10 at 23:19










  • Eager vs lazy is orthogonal to includes vs join. Includes implicitly joins. If you do N+1 instead of avoiding it then obviously specify N query is "eager" in the informal sense that you evaluate it before the +1 query. The use of "eager" in quote 3 is wrong--they mean something like, simultaneously specify the N & (via the join) the +1. PS Please give a link.
    – philipxy
    Nov 10 at 23:21












  • i think we're confusing with eager-load vs eager-fetch in third quote. i'm sure the writer would have used eager-load as that term is more common; There has to be a reason why eager-fetch is used. Using joins, yes I do see how data for associated objects can be eager fetched. The sentence actually does say "how data for associated objects"; Building the associated objects through joins was probably out of scope for this quote.
    – vee
    Nov 11 at 3:29












  • Could be that the author is talking about selecting from the joined table without loading the association. Foo.select(“foos.*, bars.baz AS baz”).joins(:bar). This can be used with aggregates like count or average. But this is not actually that common.
    – max
    Nov 11 at 9:48












  • @philipxy- can you clarify what kind of link you mean? The 3 quotes are from a book, not from a page I can link to. I could link to the book's page on Amazon, if that will help, but I did mention the book's title already so I'm not sure how much added benefit that would be.
    – Richie Thomas
    Nov 11 at 18:27















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm reading "The Rails 5 Way" now, and I've reached the section covering includes and joins. Up until now my understanding has been that includes is best-suited for eager-loading DB data into memory in order to avoid N+1 queries, while joins is more appropriate for doing simple filtering of a query result set based on associated tables, without actually accessing the attributes on those associated table rows:



1




includes(*associations)
Active Record has the capability to eliminate
“N+1” queries by letting you specify what associations to eager load
using the includes method or option in your finders. Active Record
will load those relationships with the minimum number of queries
possible.




2




joins(expression) joins works similarly to includes using an INNER
JOIN in the resulting SQL query. One of the key bits of knowledge to
understand about inner joins is they return only the set of records
that match the tables being joined. If a row on either side of the
join is missing its corresponding row on the other side, neither will
be returned in the result set.




However, on page 202 I see the following:



3




By far the most common usage of the joins method is to eager-fetch
data for associated objects in a single SELECT statement in order to
prevent so-called N+1 queries.




The usage described in quote #3 seems to be a closer fit with quote #1 than quote #2, i.e. it talks about the goal of reducing N+1 queries by eager-loading the data (which I know includes does by calling either eager_load or preload under the hood). To be honest, quote #3 doesn't appear to match #1 at all.



My question is- how can we reconcile these 3 quotes with each other? Or are they already in sync with each other and I'm simply missing something?










share|improve this question















I'm reading "The Rails 5 Way" now, and I've reached the section covering includes and joins. Up until now my understanding has been that includes is best-suited for eager-loading DB data into memory in order to avoid N+1 queries, while joins is more appropriate for doing simple filtering of a query result set based on associated tables, without actually accessing the attributes on those associated table rows:



1




includes(*associations)
Active Record has the capability to eliminate
“N+1” queries by letting you specify what associations to eager load
using the includes method or option in your finders. Active Record
will load those relationships with the minimum number of queries
possible.




2




joins(expression) joins works similarly to includes using an INNER
JOIN in the resulting SQL query. One of the key bits of knowledge to
understand about inner joins is they return only the set of records
that match the tables being joined. If a row on either side of the
join is missing its corresponding row on the other side, neither will
be returned in the result set.




However, on page 202 I see the following:



3




By far the most common usage of the joins method is to eager-fetch
data for associated objects in a single SELECT statement in order to
prevent so-called N+1 queries.




The usage described in quote #3 seems to be a closer fit with quote #1 than quote #2, i.e. it talks about the goal of reducing N+1 queries by eager-loading the data (which I know includes does by calling either eager_load or preload under the hood). To be honest, quote #3 doesn't appear to match #1 at all.



My question is- how can we reconcile these 3 quotes with each other? Or are they already in sync with each other and I'm simply missing something?







ruby-on-rails join rails-activerecord






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:23

























asked Nov 10 at 23:01









Richie Thomas

1,17022036




1,17022036








  • 4




    Third quote is simply incorrect. Joins does not load associated records.
    – Marcin Kołodziej
    Nov 10 at 23:19










  • Eager vs lazy is orthogonal to includes vs join. Includes implicitly joins. If you do N+1 instead of avoiding it then obviously specify N query is "eager" in the informal sense that you evaluate it before the +1 query. The use of "eager" in quote 3 is wrong--they mean something like, simultaneously specify the N & (via the join) the +1. PS Please give a link.
    – philipxy
    Nov 10 at 23:21












  • i think we're confusing with eager-load vs eager-fetch in third quote. i'm sure the writer would have used eager-load as that term is more common; There has to be a reason why eager-fetch is used. Using joins, yes I do see how data for associated objects can be eager fetched. The sentence actually does say "how data for associated objects"; Building the associated objects through joins was probably out of scope for this quote.
    – vee
    Nov 11 at 3:29












  • Could be that the author is talking about selecting from the joined table without loading the association. Foo.select(“foos.*, bars.baz AS baz”).joins(:bar). This can be used with aggregates like count or average. But this is not actually that common.
    – max
    Nov 11 at 9:48












  • @philipxy- can you clarify what kind of link you mean? The 3 quotes are from a book, not from a page I can link to. I could link to the book's page on Amazon, if that will help, but I did mention the book's title already so I'm not sure how much added benefit that would be.
    – Richie Thomas
    Nov 11 at 18:27
















  • 4




    Third quote is simply incorrect. Joins does not load associated records.
    – Marcin Kołodziej
    Nov 10 at 23:19










  • Eager vs lazy is orthogonal to includes vs join. Includes implicitly joins. If you do N+1 instead of avoiding it then obviously specify N query is "eager" in the informal sense that you evaluate it before the +1 query. The use of "eager" in quote 3 is wrong--they mean something like, simultaneously specify the N & (via the join) the +1. PS Please give a link.
    – philipxy
    Nov 10 at 23:21












  • i think we're confusing with eager-load vs eager-fetch in third quote. i'm sure the writer would have used eager-load as that term is more common; There has to be a reason why eager-fetch is used. Using joins, yes I do see how data for associated objects can be eager fetched. The sentence actually does say "how data for associated objects"; Building the associated objects through joins was probably out of scope for this quote.
    – vee
    Nov 11 at 3:29












  • Could be that the author is talking about selecting from the joined table without loading the association. Foo.select(“foos.*, bars.baz AS baz”).joins(:bar). This can be used with aggregates like count or average. But this is not actually that common.
    – max
    Nov 11 at 9:48












  • @philipxy- can you clarify what kind of link you mean? The 3 quotes are from a book, not from a page I can link to. I could link to the book's page on Amazon, if that will help, but I did mention the book's title already so I'm not sure how much added benefit that would be.
    – Richie Thomas
    Nov 11 at 18:27










4




4




Third quote is simply incorrect. Joins does not load associated records.
– Marcin Kołodziej
Nov 10 at 23:19




Third quote is simply incorrect. Joins does not load associated records.
– Marcin Kołodziej
Nov 10 at 23:19












Eager vs lazy is orthogonal to includes vs join. Includes implicitly joins. If you do N+1 instead of avoiding it then obviously specify N query is "eager" in the informal sense that you evaluate it before the +1 query. The use of "eager" in quote 3 is wrong--they mean something like, simultaneously specify the N & (via the join) the +1. PS Please give a link.
– philipxy
Nov 10 at 23:21






Eager vs lazy is orthogonal to includes vs join. Includes implicitly joins. If you do N+1 instead of avoiding it then obviously specify N query is "eager" in the informal sense that you evaluate it before the +1 query. The use of "eager" in quote 3 is wrong--they mean something like, simultaneously specify the N & (via the join) the +1. PS Please give a link.
– philipxy
Nov 10 at 23:21














i think we're confusing with eager-load vs eager-fetch in third quote. i'm sure the writer would have used eager-load as that term is more common; There has to be a reason why eager-fetch is used. Using joins, yes I do see how data for associated objects can be eager fetched. The sentence actually does say "how data for associated objects"; Building the associated objects through joins was probably out of scope for this quote.
– vee
Nov 11 at 3:29






i think we're confusing with eager-load vs eager-fetch in third quote. i'm sure the writer would have used eager-load as that term is more common; There has to be a reason why eager-fetch is used. Using joins, yes I do see how data for associated objects can be eager fetched. The sentence actually does say "how data for associated objects"; Building the associated objects through joins was probably out of scope for this quote.
– vee
Nov 11 at 3:29














Could be that the author is talking about selecting from the joined table without loading the association. Foo.select(“foos.*, bars.baz AS baz”).joins(:bar). This can be used with aggregates like count or average. But this is not actually that common.
– max
Nov 11 at 9:48






Could be that the author is talking about selecting from the joined table without loading the association. Foo.select(“foos.*, bars.baz AS baz”).joins(:bar). This can be used with aggregates like count or average. But this is not actually that common.
– max
Nov 11 at 9:48














@philipxy- can you clarify what kind of link you mean? The 3 quotes are from a book, not from a page I can link to. I could link to the book's page on Amazon, if that will help, but I did mention the book's title already so I'm not sure how much added benefit that would be.
– Richie Thomas
Nov 11 at 18:27






@philipxy- can you clarify what kind of link you mean? The 3 quotes are from a book, not from a page I can link to. I could link to the book's page on Amazon, if that will help, but I did mention the book's title already so I'm not sure how much added benefit that would be.
– Richie Thomas
Nov 11 at 18:27



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244265%2fdo-these-two-statements-in-the-rails-5-way-contradict-each-other%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244265%2fdo-these-two-statements-in-the-rails-5-way-contradict-each-other%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."