Rails Complex Model Association, Shared Document Between Users and Teams of Users

I have a relatively complex model association in mind, and was wondering how I could accomplish it. In essence, this is what i want to accomplish.

  • I have a User model, and a Document model
  • User A can create a document. He is now the document admin.
  1. He can then add other individual users to his document, and assign them permissions, ex: (Editor, Viewer, Admins)
  2. He can also create a team, a group of users, and add multiple teams to his document. Each user on a team that User A has added to his document will also have a level of permissions. A user can belong to many teams.

I am a little bit confused about the associations I will have to setup. This is the code I have so far, which has not incorporated the team aspect:

class User < ApplicationRecord
  has_many :participations
  has_many :documents, through: :participations
end

class Document < ApplicationRecord
  has_many :participations
  has_many :users, through: :participations
end

class Participation < ApplicationRecord
  belongs_to :user
  belongs_to :document
  enum role: [ :admin, :editor, :viewer ]
end