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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:10:09+00:00 2026-06-04T23:10:09+00:00

I need to make a where query from an array where each member of

  • 0

I need to make a where query from an array where each member of the array is a ‘like’ operation that is ANDed. Example:

SELECT ... WHERE property like '%something%' AND property like '%somethingelse%' AND ...

It’s easy enough to do using the ActiveRecord where function but I’m unsure how to sanitize it first. I obviously can’t just create a string and stuff it in the where function, but there doesn’t seem to be a way possible using the ?.

Thanks

  • 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-04T23:10:11+00:00Added an answer on June 4, 2026 at 11:10 pm

    The easiest way to build your LIKE patterns is string interpolation:

    where('property like ?', "%#{str}%")
    

    and if you have all your strings in an array then you can use ActiveRecord’s query chaining and inject to build your final query:

    a = %w[your strings go here]
    q = a.inject(YourModel) { |q, str| q.where('property like ?', "%#{str}%") }
    

    Then you can q.all or q.limit(11) or whatever you need to do to get your final result.


    Here’s a quick tutorial on how this works; you should review the Active Record Query Interface Guide and the Enumerable documentation as well.

    If you had two things (a and b) to match, you could do this:

    q = Model.where('p like ?', "%#{a}%").where('p like ?', "%#{b}%")
    

    The where method returns an object that supports all the usual query methods so you can chain calls as M.where(...).where(...)... as needed; the other query methods (such as order, limit, …) return the same sort of object so you can chain those as well:

    M.where(...).limit(11).where(...).order(...)
    

    You have an array of things to LIKE against and you want to apply where to the model class, then apply where to what that returns, then again until you’ve used up your array. Thing that look like a feedback loop tend to call for inject (AKA reduce from “map-reduce” fame):

    inject(initial) {| memo, obj | block } → obj

    Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

    If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element […] the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

    So inject takes the block’s output (which is the return value of where in our case) and feeds that as an input to the next execution of the block. If you have an array and you inject on it:

    a = [1, 2, 3]
    r = a.inject(init) { |memo, n| memo.m(n) }
    

    then that’s the same as this:

    r = init.m(1).m(2).m(3)
    

    Or, in pseudocode:

    r = init
    for n in a
        r = r.m(n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a query like this: SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId
I'm playing with GAE, and need to make a query with something like this:
How can I make a mysql dump for table from a query? I need
I need to make a query to one of Google's services. I read this
So I need to make 3 tabs with query (got this part down), but
I need a SQL query to make all data in a column UPPER CASE?
I have two tables which I need to select all rows from where the
I am making a multi-table fulltext query.But I met some question. I need make
$query = mysql_query(SELECT * FROM fruit WHERE color='red' ); while ($row = mysql_fetch_array($query)) {
I know I am making rookie errors here, and that I need to make

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.