All threads / Rails Complex Model Association, Shared Document Between Users and Teams of Users
Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Ibraheem asked in Rails

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.
  • He can then add other individual users to his document, and assign them permissions, ex: (Editor, Viewer, Admins)
  • 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
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 70,078+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.