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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:18:54+00:00 2026-06-18T06:18:54+00:00

I have an array that contains some conditions, say: ages = [1, 4, 10]

  • 0

I have an array that contains some conditions, say:
ages = [1, 4, 10].

I am trying to build a query where it will return the ages in the array. The array can be of arbitrary length. So something like:

Person.where("age == :ages", {:ages => ages})

Now this obviously does not work as :ages would be an array, when according to the equality statement above, it’s expecting a string.

I’m trying to have it achieve something along the lines of: WHERE age = 1 AND age = 4 AND age = 10 according to the ages array.

All examples online discuss how to use multiple where conditions when they are separate variables, in which case is easy as you would do: Person.where("condition1 = :first AND condition2 = :second....). I have unknown number of items in the array and I want to filter all my query result by them.

  • 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-18T06:18:55+00:00Added an answer on June 18, 2026 at 6:18 am

    It is already supported by ActiveRecord and the where statement:

    ages = [1, 5, 12]
    Person.where(age: ages)
    # This will generates a query like:
    # SELECT * FROM persons WHERE age IN(1, 5 ,12)
    

    This way of querying is also better than ‘hard-coding’ condition (passing strings to the where statement). By using Hash parameters you let ActiveRecord deal with all the DB-Query translation work.

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

Sidebar

Related Questions

I have an array-list that contains some UserID. I need a query like this:
Let's say I have a String array that contains some letters and punctuation String
I have an Array that contains some elements multiple times. Now I want to
I have an array set inside a velocity template that contains some paths. The
I have an array ($configOptions) which contains some arrays. Each array ($option) in that
I have an array that contains @sign in the object. I can't access that
I have a struct-array that contains details of different reports that can be run.
Lets say I have an array numbers that contains the following values: int numbers
I have a string array that contains some information. Example: String [] testStringArray; testStringArray[0]=
Let's say I have an Array that contains more than three repetitions of a

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.