The following sorting of my Conversation records by the latest ChatMessage works in SQLite, however, not in PostgreSQL. How can I get it to work there?
Conversation.includes(:chat_messages).order("chat_messages.created_at DESC")
class Conversation
has_many :chat_messages
end
Try to add references(:chat_messages). This will force ActiveRecord to select result by one query:
Conversation.
includes(:chat_messages).references(:chat_messages).
order("chat_messages.created_at DESC")
Read details in documentation.