我有一个使用 Ransack 构建的工作搜索表单,其中有两个单独的搜索字段用于两个不同的类,如下所示:
<%= search_form_for @q do |f| %>
<%= f.label :tags_id_in, 'Tags' %>
<%= f.select :tags_id_in, Tag.all.map{ |u| [u.name, u.id] }, { include_blank: "Tags" } %>
<%= f.label :sector_id_eq, 'Sector' %>
<%= f.select :sector_id_eq, Sector.all.map { |w| [w.name, w.id] }, {include_blank: 'Any'} %>
<%= f.submit "Search" %>
<% end %>
这些类中的每一个都链接到一个公司类
class Company < ApplicationRecord
belongs_to :sector
has_many :company_tags
has_many :tags, through: :company_tags
accepts_nested_attributes_for :company_tags
end
我正在尝试将两个选择字段合并为一个。到目前为止,我可以像这样构建 View :
<% combined = Sector.all + Tag.all %>
<%= f.select :combined, combined.map { |w| [w.name, w.id] }.sort, {include_blank: 'Any'} %>
虽然上面显示的是单个搜索表单,但它没有任何功能。有人能帮忙吗?提前致谢。
最佳答案
万岁,兰萨克。似乎 2013 年的这个答案仍然有效: Search multiple models at once with Ransack
但如果你喜欢冒险,也许试试这个: https://github.com/activerecord-hackery/ransack/issues/131
You can add associations' columns/ransackers to the list of attributes by passing their names to #attribute_select via the :associations option.
关于ruby-on-rails - Ransack 搜索多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813370/