[英]Rails - Model Associations Question
我有這些模型:
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
我想創建 model Evaluation
。 它將需要項目所有者的 ID、此項目的成員的 ID 和項目的 ID。 更好地解釋,項目的所有者將一一評估他的所有成員。 成員將只評估項目的所有者。 表 Evaluation 將有很多屬性加上前面提到的那些 Id。
我的問題是:我的模型將如何通過評估使其成為 function,以及 model 評估本身如何? 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 ...
創建 2 個評估類,一個用於 EmployeeEvaluation,一個用於 ManagerEvaluation。 使用單表 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.