PostgreSQL的,Rails和:为了=>问题 [英] PostgreSQL, Rails and :order => problem

查看:155
本文介绍了PostgreSQL的,Rails和:为了=>问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ActiveRecord模型以下行:

I have the following line in my ActiveRecord model:

class Record < ActiveRecord::Base
    has_many :users, :through => :record_users, :uniq => true, :order => "record_users.index ASC"

这是为了让我读出record.users在我为了在record_users模型中使用索引字段的方式。

This is intended to enable me to read out record.users in a way that I order using an index field in the record_users model.

现在的问题是,这种失败在PostgreSQL上,出现以下错误:

The problem is that this fails on PostgreSQL with the following error:

ActionView::TemplateError (PGError: ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list

有没有办法解决的声明,使其工作?

Is there a way to fix the statement to make it work?

推荐答案

我想你可以把它在ActiveRecord的一个bug。 PosgreSQL是更严格的比MySQL一点。你可以帮忙ActiveRecord的设立而不是像这样的联想:

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"

这篇关于PostgreSQL的,Rails和:为了=&GT;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆