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 6170825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:06:26+00:00 2026-05-23T23:06:26+00:00

I want to do something a bit like – @apples = Fruit.find :all, :conditions

  • 0

I want to do something a bit like –

@apples  = Fruit.find :all, :conditions => ["type = ?", "apple"]
@pears   = Fruit.find :all, :conditions => ["type = ?", "pear"]
@tomatos = Fruit.find :all, :conditions => ["type = ?", "tomato"]

The problem is this takes 3 DB requests. Is there a way to combine these so it only makes 1 request but gets all the required info?

Thanks,
Alex

  • 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-23T23:06:27+00:00Added an answer on May 23, 2026 at 11:06 pm

    Like said already in the comment, running a big query and fetching all entries and then selecting them via Ruby can be a pain in the ass. There a few reasons to let MySQL (or whatever database you are using) do the job:

    1. If you have 1 million entries, Ruby have to instantiate 1 million ActiveRecord objects. This eats up memory very fast, and can make your application unusable, especially in production. Imagine 100 users accessing your site, and each of your dynos (hopefully you have more than one) has to load 1 million entries and compare 1 million strings!

    2. Let the database do their jobs. They like their jobs, and they are really good at it. In fact they are optimized to run such queries. If your database seems to be slow, check your indices and tweak them if necessary.

    3. It makes your code ugly and maybe even unreadable having a big query and then splitting it up, instead of 3 variables, each defined and fetched in it’s own row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my website, I want to make something that works a bit like the
I want something like a canvas, but where i'd be able to manipulate pixels
I want something like this: abcdab.search(/a/g) //return [0,4] Is it possible?
:bn , :bp just switches buffers I want something like ctrl-o, which has a
How do I reset my numbers after they count? I want something like an
I want to do something like this within an MS Access query, but SUBSTRING
What I currently have looks a bit like this: if(userLikesBananas) { return from fruit
What I'm trying to do is to implement something like the fruit machine scroll
I would like to write something which looks a bit like what I currently
I want something like a rich-text editor for my website that is for code

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.