[Rails] How to search by multiple values ​​with LIKE

How to search by multiple values ​​with LIKE

Make a note of how to search by multiple values ​​with LIKE.

gem 'activerecord-like'It's easier to do with, but I've implemented it as an alternative.


scope = User.all
names = ['yama', 'mae']
search_sql = []

names = names.map do |n|
    search_sql.push('name like ?')
    "%#{name}%"
end

scope = scope.where( search_sql.join(' or '), *names )

Recommended Posts

[Rails] How to search by multiple values ​​with LIKE
How to search multiple columns with gem ransack
How to implement search function with rails (multiple columns are also supported)
How to get along with Rails
How to implement search functionality in Rails
[Rails] How to use rails console with docker
How to build Rails 6 environment with Docker
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] How to log in with a name by adding a devise name column
How to compare only the time with Rails (from what time to what time, something like)
[Rails] How to decide the destination by "rails routes"
How to save to multiple tables with one input
How to output CSV created by Rails to S3
How to create multiple pull-down menus with ActiveHash
How to separate .scss by controller in Rails
How to create search conditions involving multiple models
[Rails] How to upload multiple images using Carrierwave
How to implement a like feature in Rails
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
How to write Rails
How to uninstall Rails
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[Rails] How to register multiple records in the intermediate table with many-to-many association
How to change the action with multiple submit buttons
How to make batch processing with Rails + Heroku configuration
How to implement a like feature in Ajax in Rails
How to push an app developed with Rails to Github
How to remove the underline displayed by Rails link_to
How to delete a new_record object built with Rails
How to make an almost static page with rails
How to manually generate a JWT with Rails Knock
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
How to boot by environment with Spring Boot of Maven
Rails learning How to implement search function using ActiveModel
How to get started with creating a Rails app
[Docker + Rails] How to deal with Rails server startup failure
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
Use multiple databases with Rails 6.0
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar