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
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?