[英]How to make RoR database query with includes/order work in PostgreSQL?
The following sorting of my Conversation records by the latest ChatMessage works in SQLite, however, not in PostgreSQL. 按照最新的ChatMessage对我的会话记录进行的以下排序在SQLite中有效,但在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)
. 尝试添加references(:chat_messages)
。 This will force ActiveRecord to select result by one query: 这将迫使ActiveRecord通过一个查询选择结果:
Conversation.
includes(:chat_messages).references(:chat_messages).
order("chat_messages.created_at DESC")
Read details in documentation . 阅读文档中的详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.