I imagine the models must be something like this:
User:
id
username
Article:
id
user_id
title
text
Comment:
id
article_id
text
So a user has many comments pertaining to their questions, only this happens across table Articles.
In the following statement you inform this to the Rails:
class User
has_many :articles
has_many :comments, through: :articles
end
Imagine you have an object @user
id 99
. When you use @user.articles
it will generate an SQL like this:
SELECT *
FROM articles
WHERE user_id = 99
No need for JOIN.
But if you do @user.comments
he will have to do a JOIN:
SELECT comments.*
FROM comments
INNER JOIN articles
ON comments.article_id = articles.id
WHERE articles.user_id = 99
Now imagine that a comment can have many answers (Replies):
class User
has_many :articles
has_many :comments, through: :articles
has_many :replies, through: :comments
end
In doing @user.replies
he’ll do two Joins:
SELECT replies.*
FROM replies
INNER JOIN comments
ON replies.comment_id = comments.id
INNER JOIN articles
ON comments.article_id = articles.id
WHERE articles.user_id = 99
Completion
Therefore, "Join table" is when there is this indirect relationship between tables.
Do you know if the excerpt refers to the use of
join table
in an SGBD?– Gustavo Cinque