具有单表继承的关联Rails模型的ORDER BY错误

时间:2012-10-05 07:49:55

标签: ruby-on-rails ruby activerecord ruby-on-rails-3.2

我有一个单表继承模型,如下所示:

class Application < ActiveRecord::Base
  has_many :snapshots, :dependent => :destroy
  has_many :foos, :dependent => :destroy
end

class Snapshot < ActiveRecord::Base
end

class Foo < Snapshot
end

Snapshot / Foo类的日期变量名称为'when' 当我添加:order =&gt; 'when'(或:when)与has_many(foos)关系时,数据库在访问对象时抛出错误。 例如application.foos.each ...

生成的查询:

SELECT "snapshots".* FROM "snapshots"  WHERE "snapshots"."type" IN ('Foo') AND "snapshots"."application_id" = 25 ORDER BY when

*错误接近“when”:语法错误

如果我手动编辑查询并将其输入rails dbconsole:

SELECT "snapshots".* FROM "snapshots"  WHERE "snapshots"."type" IN ('Foo') AND "snapshots"."application_id" = 25 ORDER BY "snapshots"."when"

查询有效。

这是一个已知的rails 3错误还是有一些特殊的语法?任何解决方法?

3 个答案:

答案 0 :(得分:0)

尝试计时

  query.order(:when)

答案 1 :(得分:0)

试试这个

SELECT *FROM snapshots where type IN ('Foo') AND application_id = 25 ORDER BY when

答案 2 :(得分:0)

尝试了许多有用之后:

@application.foos.order("\"snapshots\".\"when\"")

使用适当的“快照”生成查询。“when”