has_and_belongs_to_many, avoiding dupes in the join table

前端 未结 12 991
南旧
南旧 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:59

    You should add an index on the tag :name property and then use the find_or_create method in the Tags#create method

    docs

提交回复
热议问题