I would like to order records by created_at
of a has_many
association and come up to the following raw MySQL query (note: articles_author_associations
and article_associations
):
SELECT
DISTINCT `articles`.*
FROM `articles`
INNER JOIN `articles_author_associations`
ON `articles`.`id` = `articles_author_associations`.`article_id`
LEFT OUTER JOIN `article_associations`
ON `article_associations`.`article_id` = `articles`.`id`
AND `article_associations`.`author_id` = 2
WHERE `articles_author_associations`.`author_id` = 2
ORDER BY `article_associations`.`created_at` DESC
LIMIT 100
The above query returns articles that are not ordered by created_at
on the LEFT OUTER JOIN
association (the article_associations
table).
My question: why? what am I doing wrong?