Rails - 基于首字母搜索和排序 [英] Rails - search and sort based on first letter
问题描述
我想根据字母列表列出用户...即当我点击字母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 Methods 和 MySQL 模式匹配 了解更多信息.(即使您使用不同的数据库,查询也应该非常相似.)
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屋!