0
@conversations = Message.find_by_sql("SELECT correspondent, MAX(updated_at) as date FROM (SELECT sender_id as correspondent,updated_at FROM messages WHERE recipient_id = #{current_user.id} UNION ALL SELECT recipient_id as correspondent, updated_at FROM messages WHERE sender_id = #{current_user.id}) x GROUP BY correspondent ORDER BY date desc")
我有這個查詢,這對我來說有點複雜,作爲初學者。我一直試圖根據活動記錄來寫這個查詢,但是我不能。寫活動記錄的複雜查詢
我有這個查詢,但這是不正確的。
@conversations = Message.where('recipient_id = ?', current_user.id).select('sender_id AS correspondent') + Message.where('sender_id = ?', current_user.id).select('recipient_id AS correspondent')
我該怎麼寫呢?