现在试图获得正确的查询一段时间。问题在于:
User
has_many :connections
Connection
belongs_to :channel
Connection
belongs_to :user
我试图通过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
。
有什么想法吗?