0

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

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 => '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

*“当”附近的错误:语法错误

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

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

查询有效。

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

4

3 回答 3

0

在尝试了很多事情之后,这很有效:

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

它使用适当的“快照”生成查询。“何时”

于 2012-10-12T00:32:40.087 回答
0

按时间试试

  query.order(:when)
于 2012-10-05T07:58:39.633 回答
0

尝试这个

SELECT *FROM snapshots where type IN ('Foo') AND application_id = 25 ORDER BY when
于 2012-10-05T08:04:05.587 回答