@@ -234,27 +234,43 @@ def self.parse_wcif_id(wcif_id)
234234 def load_live_results ( results_wcif )
235235 live_results . destroy_all
236236 registrations_by_wcif_id = competition . registrations . index_by ( &:registrant_id )
237- results_to_load = results_wcif . map do |result_wcif |
238- live_attempts = result_wcif [ "attempts" ] . map . with_index ( 1 ) do |rr , i |
239- LiveAttempt . build_with_history_entry ( rr [ "result" ] , i , 1 )
240- end
237+ now = current_time_from_proper_timezone
241238
239+ results_to_load = results_wcif . map do |result_wcif |
242240 {
243241 registration_id : registrations_by_wcif_id [ result_wcif [ "personId" ] ] . id ,
244242 round_id : self . id ,
245- live_attempts : live_attempts ,
246- last_attempt_entered_at : Time . now . utc ,
243+ last_attempt_entered_at : now ,
247244 best : result_wcif [ "best" ] ,
248245 average : result_wcif [ "average" ] ,
249246 global_pos : result_wcif [ "ranking" ] ,
250247 local_pos : result_wcif [ "ranking" ] ,
248+ created_at : now ,
249+ updated_at : now ,
251250 }
252251 end
253- LiveResult . skip_callback ( :create , :after , :recompute_local_pos )
254- LiveResult . skip_callback ( :create , :after , :recompute_global_pos )
255- LiveResult . create ( results_to_load )
256- LiveResult . set_callback ( :create , :after , :recompute_local_pos )
257- LiveResult . set_callback ( :create , :after , :recompute_global_pos )
252+
253+ LiveResult . insert_all! ( results_to_load )
254+
255+ # Reload to get the generated IDs
256+ results_by_registration_id = live_results . reload . index_by ( &:registration_id )
257+
258+ attempts_to_load = results_wcif . flat_map do |result_wcif |
259+ registration_id = registrations_by_wcif_id [ result_wcif [ "personId" ] ] . id
260+ live_result_id = results_by_registration_id [ registration_id ] . id
261+
262+ result_wcif [ "attempts" ] . map . with_index ( 1 ) do |attempt , attempt_number |
263+ LiveAttempt . build_with_history_entry ( attempt [ "result" ] , attempt_number , 1 )
264+ . attributes
265+ . merge (
266+ live_result_id : live_result_id ,
267+ created_at : now ,
268+ updated_at : now ,
269+ )
270+ end
271+ end
272+
273+ LiveAttempt . insert_all! ( attempts_to_load ) if attempts_to_load . any?
258274 end
259275
260276 def self . wcif_to_round_attributes ( event , wcif , round_number , total_rounds )
0 commit comments