ActiveRecord,PostgreSQL:group_by和同一查询中的顺序

时间:2017-06-12 20:37:22

标签: ruby-on-rails postgresql activerecord

现在试图获得正确的查询一段时间。问题在于:

  1. User has_many :connections
  2. Connection belongs_to :channel
  3. Connection belongs_to :user
  4. 我试图通过channel_id唯一的最新连接获取频道。我已经接近了:

    Connection.includes(:channel).select('DISTINCT on (channel_id) *').where(user_id: u.id).limit(5).collect(&:channel)
    

    但是当我在那里扔order时:

    Connection.includes(:channel).select('DISTINCT on (channel_id) *, created_at').order('created_at DESC').where(user_id: u.id).limit(5).collect(&:channel)
    

    我得到:SELECT DISTINCT ON expressions must match initial ORDER BY expressions

    有什么想法吗?

0 个答案:

没有答案