什么可以改变 Active Record 关系的默认顺序?

What can change the default order of an Active Record relation?

我在 Heroku 上有一个 Rails 4 的小项目。在视图中有一个 table,它的内容经常使用 Model.all.each. 显示——我想是在模型更新的时候——table 的顺序发生了变化。令人不安的是,我无法在本地主机上重现这个怪癖。虽然纠正起来很容易,但我担心它可能是其他一些未发现问题的征兆。

[编辑:该行为仅在迭代期间出现。如果我索引到集合中,它 returns 预期的元素]

[第二次编辑:从第一次编辑开始。我曾假设 Model.first 等同于 Model.all[0], 但是 运行ning 来自 Heroku 的控制台向我展示了它显然不是:第一个给我 ID 为 1 的记录,第二个给我 ID 为 10 的记录(当我 运行 each 时第一个出现的记录)。

难道您没有在开发中使用 postgres,而是在 Heroku 上这样做,因为这是默认设置?

来自 postgres 的文档:

If sorting is not chosen, the rows will be returned in an unspecified order. The actual order in that case will depend on the scan and join plan types and the order on disk, but it must not be relied on.

http://www.postgresql.org/docs/9.1/static/queries-order.html