1111# patient_id :bigint not null
1212# sent_by_user_id :bigint
1313# session_id :bigint
14- # team_location_id :bigint
14+ # team_location_id :bigint not null
1515#
1616# Indexes
1717#
@@ -35,29 +35,13 @@ class ConsentNotification < ApplicationRecord
3535
3636 belongs_to :patient
3737 belongs_to :session , optional : true
38- belongs_to :team_location , optional : true
38+ belongs_to :team_location
3939
40- scope :left_joins_session_team_location , -> { joins ( <<-SQL ) }
41- LEFT JOIN sessions
42- ON sessions.id = consent_notifications.session_id
43- LEFT JOIN team_locations AS session_team_locations
44- ON session_team_locations.id = sessions.team_location_id
45- SQL
40+ has_one :team , through : :team_location
4641
4742 scope :for_academic_year ,
4843 -> ( academic_year ) do
49- joined_scope =
50- left_joins ( :team_location ) . left_joins_session_team_location
51-
52- joined_scope . where (
53- "session_team_locations.academic_year = ?" ,
54- academic_year
55- ) . or (
56- joined_scope . where (
57- "team_locations.academic_year = ?" ,
58- academic_year
59- )
60- )
44+ joins ( :team_location ) . where ( team_locations : { academic_year : } )
6145 end
6246
6347 enum :type ,
@@ -66,9 +50,7 @@ class ConsentNotification < ApplicationRecord
6650
6751 scope :reminder , -> { initial_reminder . or ( subsequent_reminder ) }
6852
69- def team = ( team_location || session ) . team
70-
71- def academic_year = ( team_location || session ) . academic_year
53+ delegate :academic_year , to : :team_location
7254
7355 def reminder? = initial_reminder? || subsequent_reminder?
7456
0 commit comments