扶手:双方名字和姓氏高效搜索 [英] Rails: Efficiently searching by both firstname and surname

查看:203
本文介绍了扶手:双方名字和姓氏高效搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个搜索框,通过名称相匹配的用户。

I'm trying to create a 'search box' that matches users by name.

的困难在于,一个用户既有姓名和姓。每个那些可以在他们的空间(如乔恩/邦乔维,或尼尔·帕特里克·/哈里斯),而我想了解一下最有效的方式,以确保搜索进行了两个名字的串联和姓字段。

The difficulty is that a user has both a firstname and a surname. Each of those can have spaces in them (eg "Jon / Bon Jovi", or "Neil Patrick / Harris"), and I'm wondering about the most efficient way to ensure the search is carried out on a concatenation of both the firstname and surname fields.

用户列表是相当大的,所以性能是一个问题。我可以扔在用户模型中的全名闪避,但我怀疑这是不是最明智的举动性能明智的。我的多列轨道指数知识薄弱,但我怀疑有通过索引带有它这样做的方法吗?

The list of users is quite large, so performance is a concern. I could just throw a "fullname" def in the user model, but I suspect this isn't the wisest move performance wise. My knowledge of multi-column rails indexes is weak, but I suspect there's a way of doing it via an index with a " " in it?

只是为了澄清,我不需要模糊匹配 - 精确匹配只能是好的...我只是需要它是两个字段的串联运行

Just to clarify, I don't need fuzzy matching - exact match only is fine...I just need it to be run on a concatenation of two fields.

干杯......

推荐答案

您可以在你的数据库名为 FULL_NAME 与普通索引创建一个新的领域,然后利用回调填充此每当保存记录/更新:

You could create a new field in your database called full_name with a regular index, then use a callback to populate this whenever the record is saved/updated:

before_save :populate_full_name

protected

def populate_full_name
  self.full_name = "#{first_name} #{last_name}"
end

这篇关于扶手:双方名字和姓氏高效搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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