Rails Ransack gem:仅使用一个 select 选项搜索多个值
Rails Ransack gem: search for multiple values by using only one select option
我使用 ransack gem 并且我有一个 select 字段 status 有多个选项
status_collection = %w(failed partial successful)
<%= search_form_for @q, :url => validation_path(:anchor => 'list'), :class => 'form-horizontal' do |f| %>
<%= f.select :status_eq, options_for_select(Validation.status_collection, @status_eq_selection), {}, {:class => 'form-control'} %>
<% end %>
在我的控制器中:
def show
@q = Validation.where(report_id: params[:id]).search(params[:q])
@validations = @q.result(distinct: true)
end
我想将选项 failed
和 partial
合并为一个 select 选项 failed
这样如果我搜索 failed
那么ransack 还应该搜索 partial
并合并两个搜索结果。
如何通过 select 选项 failed
和属性 partial
搜索状态属性,以便我得到 failed
和 partial
通过仅搜索 failed
.
根据我们在评论中的讨论,我们可以这样做(未测试):
before_action :adjust_ransack_params, only: [:show]
private
def adjust_ransack_params
# Only change it if the status_eq is failed
if params.dig(:q, :status_eq) == 'failed'
params[:q].delete(:status_eq)
params[:q][:status_in] = %w(failed partial)
end
end
我使用 ransack gem 并且我有一个 select 字段 status 有多个选项
status_collection = %w(failed partial successful)
<%= search_form_for @q, :url => validation_path(:anchor => 'list'), :class => 'form-horizontal' do |f| %>
<%= f.select :status_eq, options_for_select(Validation.status_collection, @status_eq_selection), {}, {:class => 'form-control'} %>
<% end %>
在我的控制器中:
def show
@q = Validation.where(report_id: params[:id]).search(params[:q])
@validations = @q.result(distinct: true)
end
我想将选项 failed
和 partial
合并为一个 select 选项 failed
这样如果我搜索 failed
那么ransack 还应该搜索 partial
并合并两个搜索结果。
如何通过 select 选项 failed
和属性 partial
搜索状态属性,以便我得到 failed
和 partial
通过仅搜索 failed
.
根据我们在评论中的讨论,我们可以这样做(未测试):
before_action :adjust_ransack_params, only: [:show]
private
def adjust_ransack_params
# Only change it if the status_eq is failed
if params.dig(:q, :status_eq) == 'failed'
params[:q].delete(:status_eq)
params[:q][:status_in] = %w(failed partial)
end
end