Can anyone Convert my Sql query into Rails Query???
SELECT DISTINCT conversation_cbgs.* FROM "conversation_cbgs"
INNER JOIN "message_cbgs"
ON "message_cbgs"."conversation_cbg_id" = "conversation_cbgs"."id"
INNER JOIN "user_message_cbgs"
ON "user_message_cbgs"."message_cbg_id" = "message_cbgs"."id"
WHERE "user_message_cbgs"."user_id" = 1
ORDER BY conversations_cbgs.updated_at DESC
Assuming you have your models built correctly – which would be helpful for those answering your questions, by the way – you would have something like:
There are other ways using scopes and merges, etc. but you haven’t posted any models for us to show you.