How do I combine results from two queries on the same model?

前端 未结 4 1987
南方客
南方客 2021-02-15 13:56

I need to return exactly ten records for use in a view. I have a highly restrictive query I\'d like to use, but I want a less restrictive query in place to fill in the results i

4条回答
  •  花落未央
    2021-02-15 14:51

    with your initial code:

    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
    

提交回复
热议问题