如何在HABTM中添加唯一认证

先创建has_many_and_belongs_to_many表结构

create_join_table :products, :categories do |t|
  t.index :product_id
  t.index :category_id
end

在model中添加下列代码

class Product < ActiveRecord::Base
  has_and_belongs_to_many, -> { uniq}
end
comments powered by Disqus