1
Good afternoon, I have the following models:
User.Rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
VALID_USERNAME_REGEX = /\A[a-zA-Z0-9]*[_|-|.]*[a-zA-Z0-9]*\z/
VALID_EMAIL_REGEX = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :name, presence: true
validates :username, presence: true,
format: {with: VALID_USERNAME_REGEX,
message: "Somente letras, numeros e simbolos (. _ -)"}
validates :email, presence: true,
format: {with: VALID_EMAIL_REGEX}
has_many :projects
has_many :users_projects
has_many :roles
has_many :roles, through: :project
has_many :shared_projects, through: :users_projects, source: :project
end
Project.Rb
class Project < ApplicationRecord
belongs_to :user
belongs_to :role
has_many :users_projects
has_many :collaborators, through: :users_projects, source: :user
has_many :boards
validates :name, presence: true
#validates :user_id, presence: true
end
Role.Rb
class Role < ApplicationRecord
has_many :projects
has_many :users, through: :projects
end
User can have 3 different roles, Product Manager, Scrum Master and Developer. The problem is that, the role of a user depends on the project he is involved in, and the same user can be a Product Manager in one, and in another he can be a Developer for example. So the role of a user is more connected to a project than to the user himself. My question is, what’s the best way to put this together? The way I did, when I go popular my bank, I get an error saying that Role is mandatory. Could someone give me a hand?