Not doing so has a few gotchas when working with relations, particularly with records which are not stored in the database. I'm excluding the related content file because it's got a very peculiar relationship with itself: the `has_one :opposite_related_content` has no inverse; the relation itself is its inverse. It's a false positive since the inverse condition is true: ``` content.opposite_related_content.opposite_related_content.object_id == content.object_id ```
37 lines
1.1 KiB
Ruby
37 lines
1.1 KiB
Ruby
class Poll
|
|
class Officer < ApplicationRecord
|
|
belongs_to :user
|
|
has_many :officer_assignments
|
|
has_many :shifts
|
|
has_many :failed_census_calls, foreign_key: :poll_officer_id, inverse_of: :poll_officer
|
|
|
|
validates :user_id, presence: true, uniqueness: true
|
|
|
|
def name
|
|
user&.name || I18n.t("shared.author_info.author_deleted")
|
|
end
|
|
|
|
def email
|
|
user&.email || I18n.t("shared.author_info.email_deleted")
|
|
end
|
|
|
|
def voting_days_assigned_polls
|
|
officer_assignments.voting_days.includes(booth_assignment: :poll).
|
|
map(&:booth_assignment).
|
|
map(&:poll).uniq.compact.
|
|
sort { |x, y| y.ends_at <=> x.ends_at }
|
|
end
|
|
|
|
def final_days_assigned_polls
|
|
officer_assignments.final.includes(booth_assignment: :poll).
|
|
map(&:booth_assignment).
|
|
map(&:poll).uniq.compact.
|
|
sort { |x, y| y.ends_at <=> x.ends_at }
|
|
end
|
|
|
|
def todays_booths
|
|
officer_assignments.by_date(Date.current).map(&:booth).uniq
|
|
end
|
|
end
|
|
end
|