I am using Ruby on Rails 3.2.2 and MySQL. I have a method (an has_many :through ActiveRecord::Associations) that generates the following SLQ query:
SELECT DISTINCT `articles`.*
FROM `articles`
INNER JOIN `articles_comments_associations` `comment_associations_articles`
ON `comment_associations_articles`.`article_id` = `articles`.`id`
INNER JOIN `articles_comments_associations`
ON `articles`.`id` = `articles_comments_associations`.`article_id`
WHERE `articles_comments_associations`.`comment_id` = 223
AND (articles_comments_associations.user_id IN (2))
I would like to understand what it means INNER JOIN 'articles_comments_associations' 'comment_associations_articles' (note: there are multiple database table statements for the INNER JOIN) and how it is possible that the SQL query works since I do not have a database table named comment_associations_articles. Is it an error (even if it works as expected)?
It is a table alias. Meaning, it is renaming the table
articles_comments_associationstocomment_associations_articlesfor the purpose of further references in the query. Any field or table can be aliased by simply giving another name following the table/field reference.