我有这些模特:
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
我想创建模型Evaluation
。它将需要项目所有者的ID,该项目成员的ID以及项目的ID。更好地解释,项目的所有者将逐一评估他的所有成员。成员将评估只是项目的所有者。评估表将具有许多属性以及之前提到的ID。
我的问题是:我的模型如何使其在评估中发挥作用,模型评估本身如何? has_and_belongs_to_many
或has_many :through
?
感谢。
被修改
在黑暗中拍摄
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
猜猜我不需要Project的ID ......
答案 0 :(得分:0)
为EmployeeEvaluation创建一个评估类,一个用于ManagerEvaluation。使用单表继承。
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