$npx -y skills add ThibautBaissac/rails_ai_agents --skill multi-tenant-setupImplements URL-based multi-tenancy with account scoping, membership patterns, and data isolation following 37signals patterns. Use when setting up multi-tenant architecture, account isolation, membership management, or when user mentions multi-tenancy, accounts, or tenant separat
| 1 | # Multi-Tenant Setup |
| 2 | |
| 3 | ## Philosophy: URL-Based Multi-Tenancy, Not Subdomain or Schema |
| 4 | |
| 5 | - URL-based: `app.myapp.com/123/boards/456` (account_id in path) |
| 6 | - `account_id` on every table (no foreign key constraints) |
| 7 | - `Current.account` set from URL params for all requests |
| 8 | - All queries scoped through `Current.account` |
| 9 | - UUIDs everywhere (prevents enumeration attacks) |
| 10 | - No default scopes (explicit scoping preferred) |
| 11 | - No Apartment gem, no subdomain routing, no schema separation |
| 12 | |
| 13 | ## Project Knowledge |
| 14 | |
| 15 | **Stack:** URL-based multi-tenancy (`/accounts/:account_id/...`), Current |
| 16 | attributes for account/user context, UUIDs for all primary keys, single |
| 17 | database with single schema. |
| 18 | |
| 19 | **Auth:** Custom passwordless with `Current.user`, users can belong to multiple |
| 20 | accounts, account membership controls access. |
| 21 | |
| 22 | **Commands:** |
| 23 | ```bash |
| 24 | rails generate model Account name:string |
| 25 | rails generate model Membership user:references account:references role:integer |
| 26 | rails generate migration AddAccountToCards account:references |
| 27 | ``` |
| 28 | |
| 29 | ## Pattern 1: Account Model and Memberships |
| 30 | |
| 31 | See @references/membership-patterns.md for full details. |
| 32 | |
| 33 | ```ruby |
| 34 | # app/models/account.rb |
| 35 | class Account < ApplicationRecord |
| 36 | has_many :memberships, dependent: :destroy |
| 37 | has_many :users, through: :memberships |
| 38 | |
| 39 | has_many :boards, dependent: :destroy |
| 40 | has_many :cards, dependent: :destroy |
| 41 | |
| 42 | validates :name, presence: true, length: { maximum: 100 } |
| 43 | |
| 44 | def member?(user) |
| 45 | users.exists?(user.id) |
| 46 | end |
| 47 | |
| 48 | def add_member(user, role: :member) |
| 49 | memberships.find_or_create_by!(user: user) do |membership| |
| 50 | membership.role = role |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | def owner |
| 55 | memberships.owner.first&.user |
| 56 | end |
| 57 | end |
| 58 | |
| 59 | # app/models/membership.rb |
| 60 | class Membership < ApplicationRecord |
| 61 | belongs_to :user |
| 62 | belongs_to :account |
| 63 | |
| 64 | enum :role, { member: 0, admin: 1, owner: 2 } |
| 65 | |
| 66 | validates :user_id, uniqueness: { scope: :account_id } |
| 67 | validates :role, presence: true |
| 68 | |
| 69 | scope :active, -> { where(active: true) } |
| 70 | end |
| 71 | |
| 72 | # app/models/user.rb |
| 73 | class User < ApplicationRecord |
| 74 | has_many :memberships, dependent: :destroy |
| 75 | has_many :accounts, through: :memberships |
| 76 | |
| 77 | def member_of?(account) |
| 78 | accounts.exists?(account.id) |
| 79 | end |
| 80 | |
| 81 | def role_in(account) |
| 82 | memberships.find_by(account: account)&.role |
| 83 | end |
| 84 | |
| 85 | def admin_of?(account) |
| 86 | memberships.find_by(account: account)&.admin? || |
| 87 | memberships.find_by(account: account)&.owner? |
| 88 | end |
| 89 | end |
| 90 | ``` |
| 91 | |
| 92 | ## Pattern 2: Current Attributes for Request Context |
| 93 | |
| 94 | ```ruby |
| 95 | # app/models/current.rb |
| 96 | class Current < ActiveSupport::CurrentAttributes |
| 97 | attribute :user, :account, :membership |
| 98 | |
| 99 | delegate :admin?, :owner?, to: :membership, allow_nil: true, prefix: true |
| 100 | |
| 101 | def member? |
| 102 | membership.present? |
| 103 | end |
| 104 | |
| 105 | def can_edit?(resource) |
| 106 | return false unless member? |
| 107 | return true if membership_admin? || membership_owner? |
| 108 | resource.respond_to?(:creator) && resource.creator == user |
| 109 | end |
| 110 | end |
| 111 | |
| 112 | # app/controllers/application_controller.rb |
| 113 | class ApplicationController < ActionController::Base |
| 114 | before_action :authenticate_user! |
| 115 | before_action :set_current_account |
| 116 | before_action :set_current_membership |
| 117 | before_action :ensure_account_member |
| 118 | |
| 119 | private |
| 120 | |
| 121 | def set_current_account |
| 122 | if params[:account_id] |
| 123 | Current.account = current_user.accounts.find(params[:account_id]) |
| 124 | end |
| 125 | rescue ActiveRecord::RecordNotFound |
| 126 | redirect_to accounts_path, alert: "Account not found or access denied" |
| 127 | end |
| 128 | |
| 129 | def set_current_membership |
| 130 | if Current.account |
| 131 | Current.membership = current_user.memberships.find_by( |
| 132 | account: Current.account |
| 133 | ) |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | def ensure_account_member |
| 138 | return unless Current.account |
| 139 | unless Current.member? |
| 140 | redirect_to accounts_path, alert: "You don't have access" |
| 141 | end |
| 142 | end |
| 143 | |
| 144 | def require_admin! |
| 145 | unless Current.membership_admin? |
| 146 | redirect_to account_path(Current.account), alert: "Admin access required" |
| 147 | end |
| 148 | end |
| 149 | end |
| 150 | ``` |
| 151 | |
| 152 | ## Pattern 3: URL-Based Routing |
| 153 | |
| 154 | ```ruby |
| 155 | # config/routes.rb |
| 156 | Rails.application.routes.draw do |
| 157 | # Auth (no account context) |
| 158 | resource :session, only: [:new, :create, :destroy] |
| 159 | |
| 160 | # Account selection |
| 161 | resources :accounts, only: [:index, :new, :create] |
| 162 | |
| 163 | # All routes within account context |
| 164 | scope "/:account_id" do |
| 165 | resource :account, only: [:show, :edit, :update, :destroy] |
| 166 | resources :memberships, only: [:index, :create, :destroy] |
| 167 | |
| 168 | resources :boards do |
| 169 | resources :cards do |
| 170 | resources :comments, only: [:create, :destroy] |
| 171 | resource :closure, only: [:create, :destr |