The personal finance app for everyone (by everyone)
Ruby
Rate exchange fix (#377)
soky srm4cd575fa238b
diff --git a/app/models/exchange_rate/provided.rb b/app/models/exchange_rate/provided.rbindex 6eadf92b8e..c4e894d26a 100644--- a/app/models/exchange_rate/provided.rb+++ b/app/models/exchange_rate/provided.rb@@ -19,12 +19,23 @@ module ExchangeRate::Providedreturn nil unless response.success? # Provider errorrate = response.data- ExchangeRate.find_or_create_by!(- from_currency: rate.from,- to_currency: rate.to,- date: rate.date,- rate: rate.rate- ) if cache+ begin+ ExchangeRate.find_or_create_by!(+ from_currency: rate.from,+ to_currency: rate.to,+ date: rate.date+ ) do |exchange_rate|+ exchange_rate.rate = rate.rate+ end if cache+ rescue ActiveRecord::RecordNotUnique+ # Race condition: another process inserted between our SELECT and INSERT+ # Retry by finding the existing record+ ExchangeRate.find_by!(+ from_currency: rate.from,+ to_currency: rate.to,+ date: rate.date+ ) if cache+ endrateend