rails – using:select(distinct)with:has_many:通过关联产生无效的SQL

User has_many :posts has_many :post_tags, :through => :posts PostTag belong_to :post belongs_to :tag scope :distincttag, :select => ('distinct post_tags.tag_id') 

使用Rails 3.0.4,我得到无效的SQL:SELECT post_tags。*,distinct tag_id …

至少有一个人遇到了同样的问题: http : //www.ruby-forum.com/topic/484938

function或错误?

谢谢

放在示波器上看起来不合适。

也许你正在努力实现这个目标:

 class PostTag < ... belong_to :post belongs_to :tag def distincttag find(:all, :select => 'distinct tag_id') end end 

编辑:现在我知道你需要什么:

 User has_many :posts has_many :post_tags, :through => :posts, :select => 'distinct tags.*' # or, if you are not worried about database overhead: has_many :post_tags, :through => :posts, :uniq => true 

参考: http : //blog.hasmanythrough.com/2006/5/6/through-gets-uniq