Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.

Before it becomes a function

def keyword
    if params[:keyword]
      keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
      keywords.each do |keyword|
        offices = Office.where('name LIKE ? OR address LIKE ? OR near_station LIKE ? OR introduction LIKE ? OR company LIKE ?',"%#{keyword}%", "%#           {keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%")
      end
    else
      pagy, offices = pagy(Office.all)
      pagy_headers_merge(pagy)
    end
    render json: offices, each_serializer: OfficeIndexSerializer, include: '**'
  end

I was wandering around with such a description. The reason is that it is possible to search with one word, but with multiple keywords (for example, with a search method such as "Tokyo Fukuoka", you can only search with the letters Fukuoka.

What I was interested in

--I'm writing an iterative process using each statement, but it doesn't seem to work in the first place. ――I'm not sure if the contents of the where clause are correct ...

Final code

  def keyword
    if params[:city_id]
      offices = Office.where(city_id: params[:city_id])
    elsif params[:keyword]
      keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
      offices = []
      keywords.each do |keyword|
        offices += Office.where('name LIKE (?) OR address LIKE (?) OR near_station LIKE (?) OR introduction LIKE (?) OR company LIKE (?)',"%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%")
      end
    else
      pagy, offices = pagy(Office.all)
      pagy_headers_merge(pagy)
    end
    render json: offices
  end

The strange thing is that I added ʻoffices = [] ``. An image of making a box to store keywords. Then I added () to ʻoffice + = Office.where`` and? In the where clause.

As a result, it was possible to establish a search function not only for a single-word keyword search such as "Tokyo" but also for multiple keywords such as "Tokyo Fukuoka Chiba".

Recommended Posts

Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.
[Rails] I tried to implement "Like function" using rails and js
I tried to implement Ajax processing of like function in Rails
[API] I tried using the zip code search API
[Java] I tried to implement Yahoo API product search
Try to implement tagging function using rails and js
I tried to implement the like function by asynchronous communication
[Rails] I tried to implement batch processing with Rake task
I tried to implement the Iterator pattern
I tried to summarize the Stream API
How to implement search function with rails (multiple columns are also supported)
[Rails] I tried to implement a transaction that combines multiple DB processes
[Rails] I tried to summarize the passion and functions of the beginners who created the share house search site!
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
How to implement the breadcrumb function using gretel
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
I tried to implement a server using Netty
[Rails] How to connect to an external API using HTTP Client (I tried connecting to Qiita API)
I want to hit the API with Rails on multiple docker-composes set up locally
I tried using the Server Push function of Servlet 4.0
I tried to implement the Euclidean algorithm in Java
[Rails] Implement search function
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
[Ruby on Rails] Since I finished all Rails Tutorial, I tried to implement an additional "stock function"
I tried unit testing Rails app using RSpec and FactoryBot
[Rails] I tried using the button_to method for the first time
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basics of kotlin and java
I tried to introduce UI animation to Pokedex using Poké API
I tried using Hotwire to make Rails 6.1 scaffold a SPA
[Rails] Function to search and list products from multi-level categories
I tried to build the environment little by little using docker
Search function using [rails] ransack
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
After learning Progate, I tried to make an SNS application using Rails in the local environment
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function
Implement a refined search function for multiple models without Rails5 gem.
I tried to integrate Docker and Maven / Netbean nicely using Jib
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
[Swift] How to implement the Twitter login function using Firebase UI ①
I tried using the cache function of Application Container Cloud Service
I tried to understand how the rails method "redirect_to" is defined
I tried to build a simple application using Dockder + Rails Scaffold
I tried to understand how the rails method "link_to" is defined
I tried to summarize the methods of Java String and StringBuilder
Rails Tutorial Extension: I tried to create an RSS feed function
I tried to display the calendar on the Eclipse console using Java.
I tried to make a group function (bulletin board) with Rails
[Rails] I will write the mechanism and method of using payjp (API) very carefully (Credit card registration)
[Rails] Keyword search in multiple tables
I tried to explain the method
I tried using Java8 Stream API
How to return Rails API mode to Rails
I summarized the points to note when using resources and resources in combination
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] I was addicted to the nginx settings when using Action Cable.
I introduced WSL2 + Ubuntu to Window10 and tried using GDC, DMD, LDC
A story I was addicted to when testing the API using MockMVC
I tried to summarize the key points of gRPC design and development
[Rails] I will explain the implementation procedure of the follow function using form_with.