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

  • Home
  • SEARCH
  • 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 8023731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:38:50+00:00 2026-06-04T22:38:50+00:00

I have a model, lets say Ad, which I plan to fetch 2 datasets

  • 0

I have a model, lets say Ad, which I plan to fetch 2 datasets from, then merge them into 1. Is this possible:

ads = Ad.where(etc).limit(5)

if ads.length < 5
  merge = Ad.where(etc).limit(remainder)

// merge both here
  • 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-06-04T22:38:53+00:00Added an answer on June 4, 2026 at 10:38 pm

    ActiveRecord allows you to do chaining. Try this out, it will execute only one query.

    ads = Ad.where(etc).limit(5)
    ads = ads.where(etc).limit(remainder)
    

    But in your case, you call the length method so it will execute two queries. However, you can still do chaining. User count method is better than length method because count will send a COUNT(*) to your database (faster). length will load all records based on the where conditions and do a count on array (slower).

    ads = Ad.where(etc).limit(5)
    if ads.count < 5
      ads = ads.where(etc).limit(remainder)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have three django model classes - lets call them A, B
I have a model in rails, lets say User , which i want to
Lets say I have a project model which has many members and many tasks.
Lets say I have model inheritance set up in the way defined below. class
Lets say a have the following Model public class FirstModel { public List<SecondModel> SecondModels
So for example ... Lets say I have a Posts model and a News
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class
Let's say I have a model class called Project, but instead of this: class
Lets say I have a Product model and my controller allows the creation of

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.