本文介绍了使用 Ransack 按全名搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我继承的一个项目上有一个 Ransack 表单,如下所示:
<%- search_field_options ||= {} -%><%- search_field_options.merge!自动完成:关闭",id:q"-%><div class="search-form"><%= search_form_for(@q, ransack_form_options) 做 |f|%><%= f.text_field search_on, search_field_options %><%= f.submit '搜索' %><%= button_tag '', class: 'cancel-search' %><%结束%>
search_on
的值为 student_first_name_or_student_last_name_or_student_email_cont
.
这适用于按名字或姓氏或电子邮件进行搜索.但是如果我想搜索全名或名字或姓氏或电子邮件怎么办?我该怎么做?
解决方案
Searching full_name with ransack (first_name & last_name)
ransacker :full_name do |parent|Arel::Nodes::NamedFunction.new('CONCAT_WS', [Arel::Nodes.build_quoted(' '), parent.table[:first_name], parent.table[:last_name]])结尾
I have a Ransack form on a project I inherited that looks like this:
<%- ransack_form_options ||= {} -%>
<%- search_field_options ||= {} -%>
<%- search_field_options.merge! autocomplete: "off", id: "q" -%>
<div class="search-form">
<%= search_form_for(@q, ransack_form_options) do |f| %>
<%= f.text_field search_on, search_field_options %>
<%= f.submit 'Search' %>
<%= button_tag '', class: 'cancel-search' %>
<% end %>
</div>
The value of search_on
is student_first_name_or_student_last_name_or_student_email_cont
.
This works for searching by first name or by last name or by email. But what if I want to search for full name or first name or last name or email? How can I do that?
解决方案
Searching full_name with ransack (first_name & last_name)
ransacker :full_name do |parent|
Arel::Nodes::NamedFunction.new('CONCAT_WS', [
Arel::Nodes.build_quoted(' '), parent.table[:first_name], parent.table[:last_name]
])
end
这篇关于使用 Ransack 按全名搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!