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:
, , , , . ( ), .