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).
** Specify related model name_related model column name
as the element name of the form tag **
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)
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.
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
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)>
[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