scottmatthewman avatar

rails-delegated-roles

Use delegated_type to implement role delegation in Rails applications, with separate models and tables for each role.

version
1.0.0

Install

/plugin install rails-delegated-roles@scottmatthewman-skills

Run it inside Claude Code after adding the marketplace with /plugin marketplace add scottmatthewman/skills.

Contents

Discovered from source

Skills 1

Rails Delegated Roles

Use delegated_type when roles have their own data, associations, or validations. If roles are just permission flags, use a simpler approach.

The Problem

A "Blob" model absorbs role-specific logic, forcing optional associations and conditional validations:

# Avoid: associations must be optional, validations need guards
class User < ApplicationRecord
  belongs_to :classroom, optional: true  # Only students have classrooms
  validates :grade_level, presence: true, if: :student?
end

The Solution

1. Role model with delegated_type

class Role < ApplicationRecord
  belongs_to :user
  delegated_type :assignable, types: %w[Administrator Tutor Student]
end

This generates scopes (Role.students) and predicates (role.student?).

2. Role-specific models

Each role owns its data with required associations:

class Student < ApplicationRecord
  has_one :role, as: :assignable
  belongs_to :classroom  # Required, not optional
  validates :grade_level, presence: true
end

3. Auto-generate User methods

module RoleHelpers
  extend ActiveSupport::Concern

  included do
    has_many :roles

    Role.assignable_types.each do |type|
      scope_name = type.tableize.tr('/', '_')
      singular = scope_name.singularize

      define_method("#{singular}?") { roles.public_send(scope_name).exists? }
      define_method(singular) { roles.public_send(scope_name).first&.assignable }
    end
  end
end

4. Migration

create_table :roles do |t|
  t.references :user, null: false, foreign_key: true
  t.references :assignable, polymorphic: true, null: false
  t.timestamps
end
add_index :roles, %i[user_id assignable_type], unique: true

Each role type gets its own table with role-specific columns.

Trade-offs

Role assignment becomes deliberate (create records, not toggle checkboxes). This aids auditability but increases complexity.

Sponsors

Details

Marketplace
scottmatthewman/skills
Source
skills/rails-delegated-roles
Category
Community

More from scottmatthewman/skills

All 2