[Rails] Keyword search in multiple tables

Introduction

I had a hard time searching multiple tables by keyword in one form, so I will share it as a memorandum.

Parent table

recipes
id
title

Child table

ingredients
id
name
recipe_id(FK)

procedure

ruby:views/recipes/search.html.erb


<%= form_with url: search_recipes_path, local: true, method: :get do |f| %>
  <%= f.text_field :keyword, placeholder: "Enter a keyword" %>
  <%= f.submit "Search" %>
<% end %>

The entered content is stored in: keyword and is received by the controller's params [: keyword].

routes.rb


resources :recipes do
  collection do
    get :search
  end
end

This time it is nested in the recipe table routing.

recipes_controller.rb


def search
  @recipes = Recipe.includes(:ingredients).references(:ingredients).search(params[:keyword])
end

Let the Recipe model include the ingredients table. If you use where after includes, you need references. Pass params to scope described in model by search method.

recipe.rb


class Recipe < ApplicationRecord
  has_many :ingredients, dependent: :destroy

  scope :search, -> (keyword) {
    where('title like :q OR name like :q', q: "%#{keyword}%") if keyword.present?
  }

end

params [: keyword] is passed as an argument to (keyword). If the keyword is empty, then @ recipes = Recipe.all.

ruby:views/recipes/search.html.erb


<%= form_with url: search_recipes_path, local: true, method: :get do |f| %>
  <%= f.text_field :keyword, placeholder: "Enter a keyword" %>
  <%= f.submit "Search" %>
<% end %>

<h1>search results</h1>
<% @recipes.each do |r| %>
  <%= r.title %>
<% end %>

This is completed. If you want to add other columns to your keyword search, use OR to add the column name to where.

Reference site

https://note.com/marikooota/n/nc7dc2868f178

How to search with multiple words ↓ https://qiita.com/tomaaaaaaaa/items/4c2beeb504da058408a4 https://qiita.com/Orangina1050/items/ca4e5dc26e0a32ee3137

Recommended Posts

[Rails] Keyword search in multiple tables
Establish a search bar in Rails ~ Join multiple tables to search
Use multiple checkboxes in Rails6!
Add a search function in Rails.
How to implement search functionality in Rails
Group_by in Rails
Rails hashtag search implementation
[Rails] How to search by multiple values ​​with LIKE
Implement post search function in Rails application (where method)
Model association in Rails
Adding columns in Rails
[Rails] Implement search function
Disable turbolinks in Rails
[Rails] Save information from one form to multiple tables
[For Rails beginners] Implemented multiple search function without Gem
CSRF measures in Rails
How to write a date comparison search in Rails
^, $ in Rails regular expression
Rails search function implementation
Use images in Rails
Understand migration in rails
Split routes.rb in Rails6
Implement markdown in Rails
[Rails] Search from multiple columns + conditions with Gem and ransack
Get UserAgent in [Rails] controller
Rails fuzzy search function implementation
[Rails] Implement User search function
Search function using [rails] ransack
Declarative transaction in Rails #ginzarb
Implement follow function in Rails
Japaneseize using i18n with Rails
[Rails 6] Implementation of search function
Implement LTI authentication in Rails
[Rails] Creating a search box
Error in rails db: migrate
Gem often used in Rails
Display Flash messages in Rails
Use multiple databases with Rails 6.0
View monthly calendar in Rails
Implement import process in Rails
Rewrite Routes in Rails Engine
Implemented hashtag search like Instagram and Twitter in Rails (no gem)
Rails API mode I tried to implement the keyword multiple search function using arrays and iterative processing.