From b05676bdda09c10a7130e0f86527a75e6535dad8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 6 Apr 2018 16:37:33 +0200 Subject: Route: Change `#calculate_costs!` to `after_commit` callback Use an `after_commit` instead of an `after_save`. The `after_save` could cause intermittent problems due to Sidekiq starting before the record/transaction actually gets committed to the database. In those cases, the record wouldn't be found and cause an error. With an `after_commit` callback, the record is sure to be committed before Sidekiq takes over. Additionally, limit the callback to `:create` and `:update` because otherwise it will be active on `:destroy` by default also. This work isn't relevant on destroy, so don't do it then. Update the tests: * Update labels * Use `build` instead of `create` because we're saving the record at the end anyway so we don't need an object that starts out persisted * Use `truncation: true` for these tests because otherwise the `commit` callback doesn't get called due to transactional teardown Refs #6407 --- app/models/chouette/route.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/models/chouette') diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index 14bfa47b6..9c722851f 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -77,7 +77,9 @@ module Chouette validates_presence_of :published_name validates_presence_of :line validates :wayback, inclusion: { in: self.wayback.values } - after_save :calculate_costs!, if: ->() { TomTom.enabled? } + after_commit :calculate_costs!, + on: [:create, :update], + if: ->() { TomTom.enabled? } def duplicate opposite=false overrides = { -- cgit v1.2.3