Dhanu Gurung
Dhanu Gurung

Reputation: 8840

Need help in building associations for my scenario

I am not well-versed in associations and don't have extensive knowledge in this section.

Following is my use-case:
There are two models:

--> Employee(as a team lead) can give ratings to his team members(employee)

Will it be a good idea to only take has_many relationship between employee to rating? Also, I am bit confused, how I will be able to show ratings of team lead and their team members rating in Team Lead login session separately.

Please help.

Upvotes: 0

Views: 15

Answers (1)

SHS
SHS

Reputation: 7744

The ratings table should have a giver_id and an employee_id column. Then:

class Employee < ActiveRecord::Base
  has_many :given_ratings, foreign_key: :giver_id, class_name: Rating
  has_many :ratings
end

class Rating < ActiveRecord::Base
  belongs_to :giver, class_name: Employee
  belongs_to :employee
end

Then for any employee, the following should work:

team_lead = # fetch employee however
team_lead.ratings # ratings given TO this employee
team_lead.given_ratings # ratings given BY this employee

Upvotes: 1

Related Questions