【问题标题】:problem with ActiveRecord/Rails ordering of query data from MySQL -- no problem in SQLite来自 MySQL 的查询数据的 ActiveRecord/Rails 排序问题——在 SQLite 中没有问题
【发布时间】:2011-06-23 18:04:33
【问题描述】:

我在 application_helper.rb 文件中有以下两个 ActiveRecord 查询:

@left_menu = Page.select('id, menu_name').where(:published => true, :left_menu => true).order("sort")

也可以写成:

@left_menu = Page.select('id, menu_name').where(:published => true, :left_menu => true).order("'sort' ASC")

和:

@left_menu = Page.find(:all, :conditions => {:published => true, :left_menu => true}, :order => :sort)

为什么第一个无法在“排序”列上排序,而第二个却没有?两者都适用于 SQLite,但只有第二个适用于 MySQL。

有什么想法吗?

【问题讨论】:

  • MySQL不能有默认的排序方向,这是可以理解的。

标签: mysql ruby-on-rails ruby sqlite activerecord


【解决方案1】:

这是订单参数中的报价。
生成的查询将(类似于)

"SELECT id, title FROM `pages` WHERE (`pages`.`pub` = 1) ORDER BY 'sort' ASC"

它的字符 ' 引用。这是错误的 sql 语法,它将按 costant 值而不是列值排序。 sqlite 允许,mysql 不允许。
尝试简单使用

Page.select('id, menu_name').where(:published => true, :left_menu => true).order("sort ASC")

订单链方法参数中没有单引号。

对不起我的英语。 祝你有美好的一天

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多