Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4108682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:41:31+00:00 2026-05-20T21:41:31+00:00

I need to return exactly ten records for use in a view. I have

  • 0

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 in case the first query doesn’t yield ten results.

Just playing around for a few minutes, and this is what I came up with, but it doesn’t work. I think it doesn’t work because merge is meant for combining queries on 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 in console, this forces the restrictions in listed_articles on the query 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’s some ridiculously easy method I’m missing in the documentation, but I haven’t found it yet.

EDIT:
What I want to do is return all the articles where listed is true out of the twenty-five most recent articles. If that doesn’t get me ten articles, I’d like to add enough articles from the most recent articles where listed is not true to get my full ten articles.

EDIT #2:
In other words, the merge method seems to string the queries together to make one long query instead of merging the results. I need the top ten results of the two queries (prioritizing listed articles), not one long query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:41:31+00:00Added an answer on May 20, 2026 at 9:41 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of PHP code which I need to return an even
in delphi7 i have a function that i need to return a array as
I usually do something like the example below when I need to return error
Working with the following code, I need to return only records where the `point'
$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have
I need a function to return a suffix for days when displaying text like
I have a number of search functions (stored procedures) which need to return results
Given the following table structure, how can I use a Linq query to return
i have a input tag which is non editable, but some times i need
I have a query like this: select data_name into v_name from data_table where data_table.type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.