简体   繁体   English

如何根据关联记录对记录进行排序

[英]How do I order records based on associated records

class Conversation < ApplicationRecord
  has_many :messages, dependent: :destroy
  belongs_to :organization

  has_one :latest_message, -> {
    order(created_at: :desc)
  }, class_name: "Message", foreign_key: "conversation_id"
end
class Message < ApplicationRecord
  belongs_to :conversation
end

How do I order conversation list with the latest_message created_at time.如何使用 latest_message created_at 时间订购对话列表。

I tried我试过了

conversations.order("latest_message.created_at desc").to_a

but I'm getting the error但我得到了错误

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "latest_message"
12:33:07 web.1       | LINE 1: ...E "conversations"."organization_id" = $1 ORDER BY latest_mes...
12:33:07 web.1       |                                                              ^
12:33:07 web.1       | 
12:33:07 web.1       | from /home/rafi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
12:33:07 web.1       | Caused by PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "latest_message"
12:33:07 web.1       | LINE 1: ...E "conversations"."organization_id" = $1 ORDER BY latest_mes...
12:33:07 web.1       |                                                              ^

possibly because the latest message is not a present in DB;可能是因为 DB 中不存在最新消息; but only in the model.但仅限于 model。

In ActiveRecord the arguments to .where , .group and .order need to be the actual table names and not associations as they generate SQL.在 ActiveRecord 中, .where到 .where 、 .group.order需要是实际的表名而不是关联,因为它们会生成 SQL。

You don't really need that association at all though:但是,您根本不需要该关联:

Conversation.joins(:messages)
            .group(:id) 
            .order("MAX(messages.created_at) DESC")

You may need to use .group('conversations.id') on some drivers like TinyTDS that will produce an ambiguous GROUP BY id query instead of GROUP BY "conversations"."id" .您可能需要在某些驱动程序(如 TinyTDS)上使用.group('conversations.id') ,这将产生一个模棱两可的GROUP BY id查询而不是GROUP BY "conversations"."id" If you want to include conversations that don't have any messages use left_joins :如果您想包含没有任何消息的对话,请使用left_joins

Conversation.left_joins(:messages)
            .group(:id) 
            .order("MAX(messages.created_at) DESC")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据记录中与指定数组中的记录相匹配的记录数量来排序记录列表? - How to a order a list of records based on how many of their associated records match records in a given array? ActiveRecord:如何通过所有关联记录查找记录? - ActiveRecord: How do I find records by all of their associated records? 根据相关记录的比较订购查询 - Order a query based on comparison of associated records 如何通过关联记录的属性订购Rails集合? - How can I order a Rails collection by the attributes of associated records? 如何根据相关记录获取所有记录 - how do get all records based on an associated record 如何过滤数据库查询以不包括基于关联表的记录? - How do I filter my database query to not include records based on an associated table? 如何在Rails中保留关联记录的顺序? - How to preserve the order of associated records in Rails? 如何在关联模型中计算满足条件的记录? - How do I count records that satisfy a condition in a associated model? 如何返回与嵌套集的子项列表关联的记录? - How do I return records associated to a list of children of a nested set? 如何按相关记录的最大值对表进行排序? - How do I sort a table by the maximum value of associated records?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM