Rails: 在 heroku 中记录不是按 id 排序的

Rails: Records are not ordered by id in heroku

我遇到了一个问题,我在本地环境中开发了我的应用程序并且一切正常,但是在我部署到 heroku 之后有些东西不正常。

我有 2 个模型 PurchasingGroup 和 GroupGoal

采购组有很多组目标,当我为采购组创建组目标时,我可以像这样在控制台中查看

PurchasingGroup.last.group_goals

结果就是这个

[#<GroupGoal id: 130, created_at: "2015-03-25 17:09:08", updated_at: "2015-03-25 17:10:37", other attributes ommitted>, 
#<GroupGoal id: 131, created_at: "2015-03-25 17:09:08", updated_at: "2015-03-25 17:18:11", other attributtes ommitted]>

这是对的,它在 heroku 和我的本地也一样工作,当更新组目标时,heroku 中记录的顺序发生变化(仅在 heroku 中)所以结果是这样的

[#<GroupGoal id: 131, created_at: "2015-03-25 17:09:08", updated_at: "2015-03-25 17:10:37", other attributes ommitted>, 
#<GroupGoal id: 130, created_at: "2015-03-25 17:09:08", updated_at: "2015-03-25 17:18:11", other attributtes ommitted]>

所以如果您现在看到顺序发生了变化,这会破坏我的应用程序的很多功能,所以我必须像这样指定 EVERYTIME

PurchasingGroup.last.group_goals.order(:id)

我必须指定 order by :id 才能使其在 heroku 中工作。知道为什么吗?

如果您依赖排序的结果,您应该总是使用.order

指定一个顺序

假设您使用的是 postgres w/Heroku,postgres 按最近更新的排序。您必须覆盖模型中的默认顺序,如 has_many: group_goals, :order => "id DESC" 或您有什么