Rails: Records are not ordered by id in heroku

删除回忆录丶 提交于 2020-01-17 05:12:52

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/29265206/rails-records-are-not-ordered-by-id-in-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!