我在继承的项目中有一个 Ransack 表单,如下所示:
<%- 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>
search_on
的值为 student_first_name_or_student_last_name_or_student_email_cont
。
这适用于按名字、姓氏或电子邮件进行搜索。但是,如果我想搜索全名 或名字或姓氏或电子邮件怎么办?我该怎么做?
最佳答案
搜索全名(名字和姓氏)
ransacker :full_name do |parent|
Arel::Nodes::NamedFunction.new('CONCAT_WS', [
Arel::Nodes.build_quoted(' '), parent.table[:first_name], parent.table[:last_name]
])
end
关于ruby-on-rails - 使用 Ransack 按全名搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31550779/