Apartment Gem を使用して Rails 4 でマルチテナント アプリを構築しています。これはサブスクリプション ベースのアプリケーションであり、プラン タイプによってユーザー数が制限されています。
プラン モデルに次の検証があります (ここにすべてを貼り付けます) が、これを検証して、アカウントが最大容量に達している場合に管理者または所有者がユーザーを招待できないようにする方法がわかりません。
Plan.rb:
class Plan < ApplicationRecord
  # Enum & Constants
  enum plan_type: [:responder, :first_responder, :patrol_pro, :guardian]
  USER_LIMITS = ActiveSupport::HashWithIndifferentAccess.new(
    #Plan Name      #Auth Users
    responder:        6,
    first_responder:  12,
    patrol_pro:       30,
    guardian:         60
  )
  # Before Actions
  # Relationships
  belongs_to :account, optional: true
  # Validations
  validate :must_be_below_user_limit
  # Custom Methods
  def user_limit
    USER_LIMITS[self.plan_type]
  end
  def must_be_below_user_limit
    if account.present? && persisted? && User.count < user_limit
     errors[:user_limit] = "can not more than #{user_limit} users"
   end
  end
end
機能的には、関連付けられたアカウントのユーザー数が plan_type で許可されている数を超えている場合、所有者がユーザーを追加できないようにしたいと考えています。もしそうなら、アップグレードしてくださいというメッセージをフラッシュしたい..
前もって感謝.. これは私の存在の悩みの種です !!