jeudi 16 septembre 2021

how can create relation with has many through in rails

I have 3 model User Project Bug. I want to create many to many relation with through. I create the relation in model i don't know it is correct or not, user have user type column which is enum type user type contain developer, manager , QA

user.user_type.manager belong to many project it has one to many relation 
user.user_type.developer has many project and many project belong to developer. it has many to many realtion 

project has many bugs and bugs belong to project 

developer has many bugs and many bugs belong to developer

bug model

class Bug < ApplicationRecord
   belongs_to :project
   has_many :developers, -> { where user_type: :Developer }, class_name: 'User', through: :project, source: :bugs

end

project model

class Project < ApplicationRecord
   has_many :bugs, dependent: :delete_all
   has_many :developers, -> { where user_type: :Developer }, class_name: 'User', through: :users, source: :project
   has_many :users  //it belong to manager_id
end

user model

class User < ApplicationRecord

   enum user_type: %i[Manager Developer QA]  
   has_many :projects
   has_many :bugs
end

developer_bug model

class DevelopersBug < ApplicationRecord
  has_many :bugs
  has_many :users
end

project_developer model

class ProjectsDeveloper < ApplicationRecord
  has_many :projects
  has_many :users
end

Aucun commentaire:

Enregistrer un commentaire