简体   繁体   English

Rails:排序关联包括而不发出新查询

[英]Rails: sort association includes without issuing new query

Person has_many: :blogs has_many: :articles has_many: :blogs has_many: :articles

Blog has_one: :person 博客has_one: :person

Problem: 问题:

person.<blogs and articles>.<order by last_updated>.last

I know that I can have a scope so that all returned blogs are automagically ordered by last time, but is there a way to return all blogs and articles sorted by time? 我知道我可以设置范围,以便所有上次返回的博客在上一次前都自动排序,但是有没有办法返回所有按时间排序的博客和文章? Essentially, I'm looking for the last updated child of a parent object. 本质上,我正在寻找父对象的最后更新的子对象。

您可以整理两个查询的结果,并对结果数组进行调用排序。

(person.blogs + person.articles).sort_by(&:last_updated).last

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM