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

时间:2018-08-30 02:19:18

标签: ruby-on-rails postgresql rails-activerecord

has_many: :blogs has_many: :articles

博客 has_one: :person

问题:

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

我知道我可以有一个范围,以便所有返回的博客在上一次被自动排序,但是有没有办法返回所有按时间排序的博客和文章?本质上,我正在寻找父对象的最新更新子代。

1 个答案:

答案 0 :(得分:0)

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

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