[RUBY] [Rails] How to search across columns of related models (parent or child) in ransack

What you want to achieve

I want to search even the columns of the model related (nested) to one model

Specifically, I would like to search not only the store name of the used clothing store but also the area name (one-to-many) and the brand name (many-to-many).

スクリーンショット 2020-07-11 17.45.47.png

Conclusion

** Specify related model name_related model column name as the element name of the form tag **

The related model is to 1 (belongs_to: hoge, and has_one: fuga)

For example, when you want to use the area name of the area model associated with the shop model as a search condition. = f. Form helper: Let element name of element name be ʻarea_name_cont`.

When disassembled area → Related model name name → Column name of the related model cont → Predicate that specifies a partial match

It will be.

= search_form_for(@q, url: shop_search_path) do |f|
  = f.text_field :area_name_cont
  #Area name of the area model associated with the shop model(name)

Related models are to many (has_many: hoges, etc.)

For example, when you want to use the brand name of the brands model associated with the shop model as a search condition. = f. Form helper: Let element name of element name be brands_name_cont.

When disassembled brands → Related model name * shop has_many: brands, so plural name → Column name of the related model cont → Predicate that specifies a partial match

It will be.

= search_form_for(@q, url: shop_search_path) do |f|
  = f.text_field :brands_name_or_genres_name_cont
  #Brand name of the brand model associated with the shop model(name)
  #The genre name of the genre model associated with the shop model(name)
  #* Note that when there are multiple linked models, the model name will be plural.

By the way, you can search multiple columns by connecting column names with _or_ etc.

For the actual usage of ransack, please refer to articles such as [Rails] Summary of various search form creation methods using ransack.

Association between models

スクリーンショット 2020-07-11 18.17.14.png

shop.rb


#shop model
  belongs_to :area, optional: true
  has_many :shop_genres
  has_many :shop_brands
  has_many :genres, through: :shop_genres
  has_many :brands, through: :shop_brands

area.rb


#area model
  has_many :shops

brand.rb


#brand model
  has_many :shop_brands
  has_many :shops, through: :shop_brands

shop_brand.rb


# shop_brand model
  belongs_to :shop
  belongs_to :brand

shop_genre.rb


# shop_genre model
  belongs_to :shop
  belongs_to :genre

How to find out the association

It is convenient to use a method called ransackable_associations.

** 1. Rails c ** in the application directory

terminal


#Run in the appropriate application directory

$ rails c
Running via Spring preloader in process 61541
Loading development environment (Rails 5.0.7.2)
[1] pry(main)> 

** 2. Execute model name.ransackable_associations **

terminal


#This time I want to investigate the relationship with the Shop model, so Shop.ransackable_If you say associations
The model associated with the Shop model is displayed
[1] pry(main)> Shop.ransackable_associations
=> ["user", "area", "shop_genres", "shop_brands", "genres", "brands"]
[2] pry(main)> 

reference

[73 recipes to easily create a search form with Ransack -026 related](http://nekorails.hatenablog.com/entry/2017/05/31/173925#026-%E9%96%A2%E9%80% A3) How to search multiple columns in parent table and child table with Ransack

Recommended Posts

[Rails] How to search across columns of related models (parent or child) in ransack
[Ruby On Rails] How to search and save the data of the parent table from the child table
[rails] How to display parent information in child view in nested relationships
[Rails] How to use select boxes in Ransack
How to search multiple columns with gem ransack
How to use JQuery in js.erb of Rails6
How to write a date comparison search in Rails
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to delete large amounts of data in Rails and concerns
How to introduce jQuery in Rails 6
How to install Swiper in Rails
[Rails] I want to send data of different models in a form
How to implement search function with rails (multiple columns are also supported)
[Ruby On Rails] How to search the contents of params using include?
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to use MySQL in Rails tutorial
How to update related models with accepts_nested_attributes_for
[rails] How to configure routing in resources
How to implement ranking functionality in Rails
How to use credentials.yml.enc introduced in Rails 5.2
graphql-ruby: How to get the name of query or mutation in controller Note
[Rails] How to get rid of flash messages in a certain amount of time
How to make a unique combination of data in the rails intermediate table
Summary of how to select elements in Selenium
[Rails] Implementation of search function using gem's ransack
[Rails] Introduction of pry-rails ~ How to debug binding.pry
How to add / remove Ruby on Rails columns
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
[Rails] How to implement unit tests for models
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to create search conditions involving multiple models
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to change the column name of the table
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
How to use CommandLineRunner in Spring Batch of Spring Boot
How to convert A to a and a to A using AND and OR in Java
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to get the contents of strong parameters
Summary of how to implement default arguments in Java
Rails learning How to implement search function using ActiveModel
[Rails] How to load JavaScript in a specific view
(Ruby on Rails6) How to create models and tables