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:
选择 post_tags.*, 不同的 tag_id...
至少有其他人遇到了同样的问题:http://www.ruby-forum.com/topic/484938
功能还是错误?
谢谢
最佳答案
放在示波器上看起来不太合适。
也许您正在尝试实现这一点:
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
关于ruby-on-rails - rails - 使用 :select(distinct) with :has_many :through association produces invalid SQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4229284/