简体   繁体   中英

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 . It will need the ID of the Project Owner, the ID of a Member of this Project, and the ID of the Project. 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.

My question is: How would my models be to make it function with evaluation, and how would be model evaluation itself? has_and_belongs_to_many or 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...

  1. Project owner evaluates member performance for a project
  2. Members will evaluate the project owner for his performance on a project
  3. So, Evaluation has an evaluator, evaluee, a project, and other attributes
  4. We also need to know the types of the evaluee and evaluator

Create 2 evaluation classes one for EmployeeEvaluation, one for ManagerEvaluation. Use Single table 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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