So I have a User model created with devise and a Project model. A Project should have one Project owner and project members. So now I have no idea how to create the relations in my models.
What I have tried is have in my User model has_many :projects
and in my Project model belongs_to :user
but the problem with this is that the project is attached to only one User so i can't add any other users to it.
I tried changing it so that Project has_many :users
but this causes my registration with devise to stop working since a user should belong to a project.
Something else I thought about doing is creating a class ProjectOwner
and a class ProjectMember
and then set that a Project has_many :ProjectMember
and has_one :ProjectOwner
. And set that a user has_many :ProjectMember
and has_many :ProjectOwner
since a User can be a project owner in many Projects and be a ProjectMember in others.
Or can I just set that a Project has an attribute of type User called ProjectOwner and another attribute of type User array that named ProjectMembers?
Since I'm pretty new to RoR, I'm not sure what is the right approach for this kind of situation.
What it looks like is you want a User, Role (with ProjectOwner and ProjectMember by STI), and Project
class User < ApplicationRecord
has_many :roles
has_many :projects, through: :roles
end
class Role < ApplicationRecord
belongs_to :user
belongs_to :project
#Code that applies to both project owners and members
end
class ProjectOwner < Role
#project owner specific code
end
class ProjectMember < Role
#project member specific code
end
class Project < ApplicationRecord
has_many :project_members
has_many :members, class_name: "User", through: :project_members
has_one :project_owner
has_many :owners, class_name: "User", through: :project_owner
end
Check out this article to help set up single table inheritance