UsersController 的索引:
@users = current_user.other_users.where("first_name like ?", "%#{params[:q]}%")
1) 搜索在文本字段中工作正常,只是未检测到第一个字符。
示例:如果我用“J”搜索“John”,我不会得到任何结果,但如果我用“o”搜索,我会得到“John”,这是我应该得到的。
2) 我想知道您如何在上面为姓氏添加附加搜索条件...? 我想像
.where("first_name like ? or last_name like ?", "%#{params[:q]}%")
可以,但不行...
感谢帮助!
最佳答案
我打赌您正在使用 Postgres 作为数据库,其中 LIKE 子句区分大小写。要在 Postgres 中搜索不区分大小写:
.where("first_name ILIKE ? or last_name ILIKE ?", "%#{params[:q]}%")
这个SO有很多背景:How do you write a case insensitive query for both MySQL and Postgres?
其他替代方案:
- https://github.com/texticle/texticle
- 存储小写版本的 full_name 并在该字段中搜索
关于jquery - Rails 与 jquery token 输入 : first character ignored in search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11779468/