2011-05-23 121 views
2

我有这些模型:导轨 - 模型关联问题

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_manyhas_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 

猜猜我将不再需要从项目的ID ...

+3

'has_and_belongs_to_many'不允许您在表格中拥有额外的属性,所以从一开始就是不行的。 – bruno077 2011-05-23 20:57:34

+0

这也是我的初衷。感谢你的回答! – Luk 2011-05-23 21:05:42

+0

您在#6133064 – Yardboy 2011-05-26 19:15:23

回答

0
  1. 项目业主评估成员的表现的一个项目
  2. 会员将评估项目业主在项目中的表现
  3. 因此,评价有一个评估,evaluee,一个项目,和其他属性
  4. 我们还需要知道的类型evaluee和评估的

创建2个评价类别之一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