Rails idiom to avoid duplicates in has_many :through

前端 未结 8 1861
难免孤独
难免孤独 2020-12-07 12:09

I have a standard many-to-many relationship between users and roles in my Rails app:

class User < ActiveRecord::Base
  has_many :user_roles
  has_many :ro         


        
8条回答
  •  没有蜡笔的小新
    2020-12-07 12:57

    Perhaps it is possible to create the validation rule

    validates_uniqueness_of :user_roles
    

    then catch the validation exception and carry on gracefully. However, this feels really hacky and is very inelegant, if even possible.

提交回复
热议问题