繁体   English   中英

Rails - Model 关联问题

[英]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_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

我猜我不需要 Project 的 ID ...

  1. 项目所有者评估项目的成员绩效
  2. 成员将评估项目所有者在项目中的表现
  3. 因此,Evaluation 具有评估者、评估者、项目和其他属性
  4. 我们还需要知道被评估者和评估者的类型

创建 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM