has_and_belongs_to_many, avoiding dupes in the join table

前端 未结 12 994
南旧
南旧 2020-12-04 11:07

I have a pretty simple HABTM set of models

class Tag < ActiveRecord::Base 
   has_and_belongs_to_many :posts
end 

class Post < ActiveRecord::Base 
           


        
12条回答
  •  感动是毒
    2020-12-04 11:43

    Set the uniq option:

    class Tag < ActiveRecord::Base 
       has_and_belongs_to_many :posts , :uniq => true
    end 
    
    class Post < ActiveRecord::Base 
       has_and_belongs_to_many :tags , :uniq => true
    

提交回复
热议问题