svelandiag
svelandiag

Reputation: 4320

Rails: Records are not ordered by id in heroku

Im having a problem I developed my app in my local environment and everything works as espected, but after I deployed to heroku something is not working right.

I have 2 models PurchasingGroup and GroupGoal

a purchasing group has many group goals, well when I create group goals for a purchasing group I can check in the console like this

PurchasingGroup.last.group_goals

and the result is this one

[#<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]>

well this is right, it works like that in heroku and in my local too, when a group goal is updated the order of the records change in heroku (ONLY in heroku) so the result is like this

[#<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]>

so if you see now the order changed, and this breaks a lot of functionality of my application so I have to specify EVERYTIME like this

PurchasingGroup.last.group_goals.order(:id)

I have to specify order by :id in order to make it work in heroku. Any idea why?

Upvotes: 0

Views: 131

Answers (2)

Ryan Rebo
Ryan Rebo

Reputation: 1318

Assuming you are using postgres w/Heroku, postgres sorts by the most recently updated. You'll have to override the default order in your model as in has_many: group_goals, :order => "id DESC", or what have you

Upvotes: 2

maček
maček

Reputation: 77786

If you depend on ordered results, you should always specify an order using .order

Upvotes: 2

Related Questions