且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何合并来自同一模型的两个查询的结果?

更新时间:2023-12-02 09:20:16

使用您的初始代码:

您可以使用 + 连接两个数组,然后获得前 10 个结果:

You can join two arrays using + then get first 10 results:

  def self.current
    (Article.listed_articles  +  Article.rescue_articles)[0..9]
  end

我想一种非常肮脏的做法是:

I suppose a really dirty way of doing it would be:

  def self.current
      oldest_accepted = Article.published.order('created_at DESC').limit(25).last
      Artcile.published.where(['created_at > ?', oldest_accepted.created_at]).order('listed DESC').limit(10)
  end