[英]Rails - Model Associations Question
I have these models:我有这些模型:
class Profile
has_many :projects, :through => "teams"
has_many :teams, :foreign_key => "member_id"
has_many :own_projects, :class_name => "Project", :foreign_key => :profile_id
has_many :own_teams, :through => :own_projects, :source => :teams
end
class Project
belongs_to :profile, :class_name => "Profile"
has_many :teams
has_many :members, :class_name => "Profile", :through => "teams", :foreign_key => "member_id"
end
class Team
belongs_to :member, :class_name => 'Profile'
belongs_to :project
end
I want to create model Evaluation
.我想创建 model
Evaluation
。 It will need the ID of the Project Owner, the ID of a Member of this Project, and the ID of the Project.它将需要项目所有者的 ID、此项目的成员的 ID 和项目的 ID。 Explaining better, The Owner of the Project will Evaluate all his members, one by one.
更好地解释,项目的所有者将一一评估他的所有成员。 And the members will evaluate just the owner of the project.
成员将只评估项目的所有者。 The table Evaluation will have a lot of attributes plus those Ids mentioned before.
表 Evaluation 将有很多属性加上前面提到的那些 Id。
My question is: How would my models be to make it function with evaluation, and how would be model evaluation itself?我的问题是:我的模型将如何通过评估使其成为 function,以及 model 评估本身如何?
has_and_belongs_to_many
or has_many:through
? has_and_belongs_to_many
或has_many:through
?
Thanks.谢谢。
Edited已编辑
Shot in the dark在黑暗中拍摄
class Evaluations < ActiveRecord::Base
belongs_to :evaluated, :class_name => 'Profile', :foreign_key => "evaluated_id"
belongs_to: :evaluator, :class_name => 'Profile', :foreign_key => "profile_id"
end
Guess I won't need the ID from Project...我猜我不需要 Project 的 ID ...
Create 2 evaluation classes one for EmployeeEvaluation, one for ManagerEvaluation.创建 2 个评估类,一个用于 EmployeeEvaluation,一个用于 ManagerEvaluation。 Use Single table inheritance.
使用单表 inheritance。
class Evaluation < ActiveRecord::Base
attr_accessible :evaluator, :evaluee
belongs_to :project
end
class EmployeeEvaluation < Evaluation #project manager evaluation of employee
def evaluator
Manager.find(self.evaluator)
end
def evaluee
Employee.find(self.evaluee)
end
end
class ManagerEvaluation < Evaluation
def evaluator
Employee.find(self.evaluator)
end
def evaluee
Manager.find(self.evaluee)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.