As long as the appended role is an ActiveRecord object, what you are doing:
user.roles << role
Should de-duplicate automatically for :has_many
associations.
For has_many :through
, try:
class User
has_many :roles, :through => :user_roles do
def <<(new_item)
super( Array(new_item) - proxy_association.owner.roles )
end
end
end
if super doesn’t work, you may need to set up an alias_method_chain.