使用 Ransack 搜索多个字段 [英] Searching multiple fields with Ransack

查看:57
本文介绍了使用 Ransack 搜索多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Ransack 来允许对我的用户进行高级搜索.目前,用户有 first_namemiddle_namelast_name 列.这是我的代码:

I'm using Ransack to allow advanced searching on my users. Currently, the users have first_name, middle_name, and last_name columns. Here is my code:

.field
  = f.attribute_fields do |a|
    = a.attribute_select
    ...

如何自定义一个名为名称"的内容,让用户搜索上述所有三列?

How can I have a custom one called 'Name', that lets the user search through all three of the columns mentioned above?

注意我仍然希望保留其余属性作为选项,例如电子邮件、电话号码等.请在确定答案时记住这一点.

Note I would still like to keep the remaining attributes as options as well, such as email, phone number, etc. Please keep this in mind when determining an answer.

推荐答案

我建议为此搜索提供一个专门的专栏.虽然这可能会产生冗余数据,但与对现有列执行一些 SQL 魔法相比,它方式更容易搜索.

I would suggest to provide a dedicated column for this search. While that might create redundant data, it is way easier to search than doing some SQL magic on the existing columns.

您可以轻松地自动设置此字段:

You can easily automate the setting of this field:

before_save :set_full_name

def set_full_name
  self.full_name = [first_name, middle_name, last_name].reject(&:blank?).join(" ")
end

然后你就可以使用普通的ransack方法来搜索这个字段了.

Then you can use the normal ransack methods to search this field.

这篇关于使用 Ransack 搜索多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆