I'm considering different designs for a generic role library gem.
The first is based off the Rolify gem but with a has_many through: relationship instead of HABTM. HABTM tables are "headless" in ActiveRecord and cannot be queried directly which is a huge con.
class User < ApplicationRecord
has_many :user_roles
has_many :roles, through: :user_roles
end
class UserRole < ApplicationRecord
belongs_to :user
belongs_to :role
end
class Role < ApplicationRecord
has_many :user_roles
has_many :users, through: :user_roles
belongs_to :resource, polymorphic: true
end
But looking at this I can't really see any distinct advantages to having a separate table vs joining users to roles directly.
class User < ApplicationRecord
has_many :roles
end
class Role < ApplicationRecord
belongs_to :user
end
Having a separate Role table lets you perhaps create "global" roles and then attach users to the role.
But is it really worth the performance hit and added complication vs letting the roles be unique per user?

name,resource_idandresource_typefor everyrole_idxuser_id. Certainly query time to search that list of duplicates would be greater than a 3 table join? Also consider that you are introducing the likelihood of data anomalies with your 2nd solution by introducing transitive dependencies to your roles table.