Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

Handle manually entered securities

Josh Pigfordc2561b5fb4b9
diff --git a/app/models/security/importer.rb b/app/models/security/importer.rb
index ed515067..4146fea2 100644
--- a/app/models/security/importer.rb
+++ b/app/models/security/importer.rb
@@ -21,6 +21,21 @@ class Security::Importer
.compact
.uniq { |security| [ security[:ticker], security[:exchange_mic] ] }
+ # First update any existing securities that only have a ticker
+ Security.where(exchange_mic: nil)
+ .where(ticker: securities_to_create.map { |s| s[:ticker] })
+ .update_all(
+ securities_to_create.map do |security|
+ {
+ name: security[:name],
+ country_code: security[:country_code],
+ exchange_mic: security[:exchange_mic],
+ exchange_acronym: security[:exchange_acronym]
+ }
+ end.first
+ )
+
+ # Then create/update any remaining securities
Security.upsert_all(
securities_to_create,
unique_by: [ :ticker, :exchange_mic ],