aalvrz/scsj

Has_many :trials association between User

Closed this issue · 1 comments

The current association only allows judges to obtain their trials, because of the specified foreign key:

class User < ApplicationRecord

  has_many :trials, foreign_key: :judge_id
end

This approach does not allow lawyers to retrieve their trials as well.

According to this question, this problem can be solved by removing the has_many association and creating a custom method for fetching the records. Like this:

class User < ApplicationRecord
  def trials
    Trial.where('judge_id = ? OR plaintiffs_lawyer_id = ?
      OR defendants_lawyer_id = ?', self.id, self.id, self.id)
  end
end