How to combine the results of two queries in one model?

I need to return exactly ten records for use in the view. I have a very strict query that I would like to use, but I want to get a less strict query to populate the results if the first query fails ten results.

Just play for a few minutes, and that’s what I came up with, but it won’t work. I think this will not work, because the merge is designed to combine requests for different models, but I could be wrong.

class Article < ActiveRecord::Base
...
  def self.listed_articles
    Article.published.order('created_at DESC').limit(25).where('listed = ?', true)
  end

  def self.rescue_articles
    Article.published.order('created_at DESC').where('listed != ?', true).limit(10)
  end

  def self.current
    Article.rescue_articles.merge(Article.listed_articles).limit(10)
  end
...
end

Looking at the console, this forces restrictions on the request risk list in rescue_articles, showing something like:

Article Load (0.2ms)  SELECT `articles`.* FROM `articles` WHERE (published = 1) AND (listed = 1) AND (listed != 1) ORDER BY created_at DESC LIMIT 4
Article Load (0.2ms)  SELECT `articles`.* FROM `articles` WHERE (published = 1) AND (listed = 1) AND (listed != 1) ORDER BY created_at DESC LIMIT 6 OFFSET 4

I'm sure there is some ridiculously easy method that I lack in the documentation, but I haven't found it yet.

EDIT: , , , , 25 . , , , .

№ 2: , , , , . ( ), .

+5
3

:

, +, 10 :

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

, :

  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
+9

, , :

result1 = Model.where(condition)
result2 = Model.where(another_condition)

# your final result
result = result1 + result2
+3

, :

 Article.published.order('listed ASC, created_at DESC').limit(10)

, . , create_at DESC, .

0

All Articles