我的項目託管在Heroku上。 當Room.all
方法返回對象first object with ID 2
,然後只有一個second object with ID 1
時,我很驚訝。我認爲有一些保證,返回的對象已經按ID排序。我是否應該始終撥打Room.all.order(:id)
而不是常規的all
方法?有沒有保證ActiveRecord返回按ID排序的對象?
irb(main):002:0> Room.all
=> #<ActiveRecord::Relation [
#<Room id: 2, color: "rgb(83, 180, 83)", status: "Status #2", created_at: "2014-10-11 14:14:02", updated_at: "2014-10-11 14:18:19">,
#<Room id: 1, color: "rgb(0, 96, 255)", status: "Status #3", created_at: "2014-10-11 14:14:02", updated_at: "2014-10-11 14:18:30">
]>
無,取決於DB」 – apneadiving 2014-10-11 15:44:25
沒有不能確定有關訂單。我一直在使用Oracle,PostgreSQL,它沒有提供有序的記錄。 MySQL似乎依次返回記錄。 – Allerin 2014-10-11 16:02:02