Sql Query Order Problem
So I've got the follow model in activerecord, I will use Discussion.user_replied_group_discussions(@user)it returns the recent replied discussions alright, but how do I modify the
Solution 1:
This will order according to latest discussions.updated_at time:
where("discussable_type = 'Group' AND id IN (#{discussion_ids})
order by updated_at DESC ",
If you want to order by comment.created_at, use this query:
SELECT d.*
, MAX(c.created_at) AS lastCommentTime
FROM discussions d
JOIN comments c
ON d.id = c.commentable_id
WHERE c.commentable_type = 'Discussion'AND c.user_id = userIDtoCheck
GROUPBY d.id
ORDERBY lastCommentTime DESC <--- latest first
Post a Comment for "Sql Query Order Problem"