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?
Best Answer
Absolutely. The advantage of having preset/saved roles that are available to be assigned to different users is:
If you're going to build a generic role library, you probably need the data management flexibility provided by a RDBMS. The many-to-many relationship you're creating is fairly common and shouldn't be that difficult to work with.