gpt4 book ai didi

ruby-on-rails - rails - 使用 :select(distinct) with :has_many :through association produces invalid SQL

转载 作者:行者123 更新时间:2023-12-05 01:15:39 30 4
gpt4 key购买 nike

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.*, 不同的 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com