Rails - using: select (distinct) with: has_many: creates invalid SQL through association

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') 

with Rails 3.0.4, I get invalid SQL: SELECT post_tags. *, Different tag_id ...

at least one other person has experienced the same problem: http://www.ruby-forum.com/topic/484938

or mistake?

thanks

+4
source share
1 answer

Unlike the correct placement of the area.

Perhaps you are trying to do this:

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

Edit: now that I know what you need:

 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 

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

+3
source

All Articles