Rails - 基于首字母搜索和排序 [英] Rails - search and sort based on first letter

查看:53
本文介绍了Rails - 基于首字母搜索和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据字母列表列出用户...即当我点击字母A"时应列出所有以字母 A 开头的用户,B、C 等的大小写相同.

I wanted to list users based on alphabets listing... i.e when I click on letter "A" it should list all the users starting with letter A, same case for B, C, etc.

为此我使用代码

@users = User.search(params[:char])

params[:char] 我传递字母(a、b、c、d 等).

in params[:char] I am passing letters (a, b, c, d, etc.).

此代码搜索带有此字母的单词.

This code searches for a word with this letter.

相反,我想要一个以特定字母开头的用户列表.

Instead I want a list of users starting with perticular letter.

请您建议如何处理.

谢谢.

推荐答案

您可能想要使用类似 SQL 的查询.

You probably want to use a SQL like query.

@users = User.all(:conditions => "name like '#{params[:char]}%'")

请参阅有关 ActiveRecord Finder MethodsMySQL 模式匹配 了解更多信息.(即使您使用不同的数据库,查询也应该非常相似.)

See the documentation on ActiveRecord Finder Methods and MySQL Pattern Matching for more information. (Even if you are using a different database, the query should be pretty similar.)

这篇关于Rails - 基于首字母搜索和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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