Rails:ActiveRecord数据库排序操作不区分大小写 [英] Rails: ActiveRecord db sort operation case insensitive

查看:69
本文介绍了Rails:ActiveRecord数据库排序操作不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Rails(通过遵循Coursera的SAAS课程)并使用ActiveRecord处理简单的Movie表。

I am trying to learn rails [by following the SAAS course in coursera] and working with simple Movie table using ActiveRecord.

我要显示标题排序的所有电影。我希望它不区分大小写地排序。

I want to display all movies with title sorted. I would like it to be sorted case insensitively.

我尝试过这种方式:

Movie.all(:conditions => ["lower(title) = ?", title.downcase],:order => "title DESC")
=>undefined local variable or method `title' for #<MoviesController:0xb4da9a8>

我认为它不能识别lower(title)。

I think it doesnt recognise lower(title) .

这是实现案例坚持式分类的最好方法吗?

Is this the best way to achieve case insesisitve sort ?

谢谢!

推荐答案

使用其中而不是 all

Movie.where("lower(title) = ?", title.downcase).order("title DESC")

虽然不是很了解。在这里,您将获得所有具有较低标题的电影,它们等于 title.downcase 。一切都相等,如何按 title desc 排序?

Don't really understand the sort though. Here you'll get all movies with lower title equalling to title.downcase. Everything is equal, how could you sort it by title desc ?

按小写字母反向排列所有电影title:

To sort reverse-alphabetically all movies by lowercase title :

Movie.order("lower(title) DESC").all

这篇关于Rails:ActiveRecord数据库排序操作不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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