ruby on rails - Is there a guarantee that ActiveRecord returns objects ordered by ID? -
my project hosted on heroku. surprised when room.all
method returned objects first object id 2
, second object id 1
. thought there sort of guarantee objects returned ordered id. should phone call room.all.order(:id)
instead of regular all
method?
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"> ]>
nope. room.all
ends sql select * rooms;
- no order there. in event, order of records determined database (for instance, in postgresql, notice returns me updated records last).
if want ensure there's order when phone call .all
, add together default scope adds it:
default_scope order('rooms.id asc')
ruby-on-rails postgresql activerecord ruby-on-rails-4 heroku
No comments:
Post a Comment