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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:16:21+00:00 2026-05-24T11:16:21+00:00

I am attempting to do a search in Rails 3 like so. I have

  • 0

I am attempting to do a search in Rails 3 like so. I have a database object like so with an attribute like so:

@food.fruit = "Apples, Pears, Plums"

and I have a search that uses params from a checkbox, like so:

params[:search] = ["Apples", "Oranges", "Bananas", "Grapefruit"]

Since my @food object above has “Apples” and the search array has “Apples” I’d like for this search to be successful. However I am having trouble having this work properly.

My initial thought was to do something like

@results = Food.where("fruit LIKE ?", "%params[:search]%")

or

@results = Food.where("fruit IN ?", params[:search])

But the first only works if the params[:search] contains ONLY the @food.fruit elements and no others. The second doesn’t work at all.

My last ditch resort is to do something like

@results = Array.new
params[:search].each do |search|
  @results << Food.where("fruit LIKE ?", search)
end

but I’d rather not do that if I don’t have to. Anyone have any advice?

  • 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-24T11:16:22+00:00Added an answer on May 24, 2026 at 11:16 am

    What you’re looking for is some SQL like this:

    WHERE LOWER(fruit) LIKE '%apples%'
       OR LOWER(fruit) LIKE '%oranges%'
       OR LOWER(fruit) LIKE '%bananas%'
       OR LOWER(fruit) LIKE '%grapefruit%'
    

    Note that LIKE is not necessarily case insensitive so pushing everything to lower case (or upper case) is generally a good idea.

    That’s simple enough but saying x.where(...).where(...)... connects the conditions with AND when you want OR. One way is to build the first argument to where as a string by pasting together the right number of "LOWER(fruit) LIKE ?" strings to match the number of elements in params[:search]:

    @results = Food.where(
        (["LOWER(fruit) LIKE ?"] * params[:search].length).join(' OR '),
        *(params[:search].map { |s| '%' + s.downcase + '%' })
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to write a search function for a database table that needs to
I have a search query that I'm inheriting and attempting to optimize. I am
I am attempting to use the following code to search a database using words
Attempting to database driven website for video rentals So I have two tables in
Using sed, I am attempting to recursively search and replace a string foo that
I am attempting to write (or expand on an existing) graph search algorithm that
I'm attempting to create a search function, built something like this: function Search(array, key,
I attempting to write a simple JQuery search. This is what I have so
I am attempting to mimic the bing video search preview feature. I have a
I'm attempting to use regexp to parse a search string that from time to

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.