PostgreSQL, Rails and :order => problem

拜拜、爱过 提交于 2019-11-30 14:03:33
Casper Fabricius

I suppose you could call it a bug in ActiveRecord. PosgreSQL is a bit more restrictive than MySQL. You can help out ActiveRecord by setting up the association like this instead:

class Record < ActiveRecord::Base
  has_many :users,
   :through => :record_users,
   :select => 'DISTINCT users.*, record_users.index',
   :order => "record_users.index ASC"

Just posted this issue on rails's issue tracker on github (copied from lighthouse ticket so we could bring it back.. it was marked invalid):

https://github.com/rails/rails/issues/520

Promote it if you want this fixed nicely! :)

I ran into something similar before and I believe it's an AR bug related to PGSQL (https://rails.lighthouseapp.com/projects/8994/tickets/1711-has-many-through-association-with-order-causes-a-sql-error-with-postgresql).

I got around it by dropping the DISTINCT (:uniq) directive and resolved the uniq records another way. Kind of a bummer though.

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