[RUBY] How to search multiple columns with gem ransack

At the beginning

I thought it would be difficult to search for multiple columns, but it was easier than I expected, so I thought it would be helpful, so I decided to write it.

Premise

I will search for the title and description of the Post model.

class CreatePosts < ActiveRecord::Migration[5.2]
  def change
    create_table :posts do |t|
      t.string :title ⬅️⬅️⬅️⬅️⬅️⬅️ This
      t.integer :recommended    
      t.text :description ⬅️⬅️⬅️⬅️ This
      t.references :user, foreign_key: true

      t.timestamps
    end
    add_index :posts, [:user_id, :created_at]
  end
end

manner

First, we will change a part of the view. If you want to search only title,

<%= f.search_field :title_cont, placeholder: "Keyword search", class: 'form-control font-awesome' %>

When searching for multiple titles and descriptions

<%= f.search_field :title_or_description_cont, placeholder: "Keyword search", class: 'form-control font-awesome' %>

By comparison, the search target for only title is title_cont, while the search target for multiple titles is title_or_description_cont . Just put or!

Next is the controller. In my case, if only the title is the search target,

def set_search
  if logged_in?
    @search_word = params[:q][:title_cont] if params[:q]
    @q = current_user.feed.page(params[:page]).per(10).ransack(params[:q])
    @feed_items = current_user.feed.page(params[:page]).per(10)
    @posts = @q.result(distinct: true)
  end
end

If you have multiple search targets, you only need to make some changes here as well as the previous view!

@search_word = params[:q][:title_or_description_cont] if params[:q]

Now you can search multiple columns! !! !!

Recommended Posts

How to search multiple columns with gem ransack
[Rails] Search from multiple columns + conditions with Gem and ransack
How to implement search function with rails (multiple columns are also supported)
[Rails] How to search by multiple values ​​with LIKE
How to save to multiple tables with one input
How to create multiple pull-down menus with ActiveHash
How to create search conditions involving multiple models
How to change the action with multiple submit buttons
How to number (number) with html.erb
How to update with activerecord-import
[Rails] How to search across columns of related models (parent or child) in ransack
How to deal with the error ERROR: While executing gem ... (Gem :: FilePermissionError)
[Rails] How to use gem "devise"
How to scroll horizontally with ScrollView
How to add columns to a table
How to get started with slim
How to enclose any character with "~"
To display multiple lines with UILabel
How to use mssql-tools with alpine
How to get along with Rails
[Swift] How to replace multiple strings
How to start Camunda with Docker
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to implement search functionality in Rails
How to compile Java with VsCode & Ant
How to read Body of Request multiple times with Spring Boot + Spring Security
[Java] How to compare with equals method
How to realize hybrid search using morphological analysis and Ngram with Solr
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
[Note] How to get started with Rspec
Introduction to algorithms with java --Search (depth-first search)
How to do API-based control with cancancan
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
Ransack sort_link How to change the color!
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
Create an or search function with Ransack.
How to deal with Precompiling assets failed.
How to switch between multiple Java versions
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to execute multiple commands in docker-compose.yml
How to build Rails 6 environment with Docker
Introduction to algorithms with java --Search (breadth-first search)
[Rails] How to register multiple records in the intermediate table with many-to-many association
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x
Introduction to algorithms with java-Search (Full search, Binary search)
Assignment to multiple variables with the ternary operator
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to add / remove Ruby on Rails columns