From 0308a67a86a5497977d0722f98ac7ba577e3c050 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Wed, 2 Mar 2016 21:11:38 -0500 Subject: [PATCH] Simplified all quickbooks inporting no more loops --- app/models/qbo_customer.rb | 13 +++++++------ app/models/qbo_employee.rb | 11 +++++------ app/models/qbo_estimate.rb | 11 +++++------ app/models/qbo_item.rb | 13 ++++++------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/app/models/qbo_customer.rb b/app/models/qbo_customer.rb index 27a7cd3..a429e28 100644 --- a/app/models/qbo_customer.rb +++ b/app/models/qbo_customer.rb @@ -25,12 +25,13 @@ class QboCustomer < ActiveRecord::Base def self.update_all customers = get_base.service.all - # Update the customer table - customers.each { |customer| - qbo_customer = QboCustomer.find_or_create_by(id: customer.id) - qbo_customer.name = customer.display_name - qbo_customer.save! - } + + ids = customers.map {|i| i.id} + display_names = customers.map {|i| i.display_name} + + # Update the customer table + find_or_create_by(id: ids, name: display_names) + #remove deleted customers where.not(customers.map(&:id)).destroy_all end diff --git a/app/models/qbo_employee.rb b/app/models/qbo_employee.rb index 76067b2..d9300d6 100644 --- a/app/models/qbo_employee.rb +++ b/app/models/qbo_employee.rb @@ -21,12 +21,11 @@ class QboEmployee < ActiveRecord::Base def self.update_all employees = get_base.service.all - # Update the item table - employees.each { |employee| - qbo_employee = QboEmployee.find_or_create_by(id: employee.id) - qbo_employee.name = employee.display_name - qbo_employee.save! - } + ids = employees.map {|i| i.id} + display_names = employees.map {|i| i.display_name} + + # Update the customer table + find_or_create_by(id: ids, name: display_names) #remove deleted employees where.not(employees.map(&:id)).destroy_all diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb index b30ded6..7d637d6 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/qbo_estimate.rb @@ -21,12 +21,11 @@ class QboEstimate < ActiveRecord::Base def self.update_all estimates = get_base.service.all - # Update the item table - estimates.each { |estimate| - qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) - qbo_estimate.doc_number = estimate.doc_number - qbo_estimate.save! - } + ids = estimates.map {|i| i.id} + doc_numbers = estimates.map {|i| i.doc_number} + + # Update the invoice table + find_or_create_by(id: ids, doc_number: doc_numbers) #remove deleted estimates where.not(estimates.map(&:id)).destroy_all diff --git a/app/models/qbo_item.rb b/app/models/qbo_item.rb index d99560b..5e79c18 100644 --- a/app/models/qbo_item.rb +++ b/app/models/qbo_item.rb @@ -20,13 +20,12 @@ class QboItem < ActiveRecord::Base def self.update_all items = get_base.service.find_by(:type, "Service") - - # Update the item table - items.each { |item| - qbo_item = QboItem.find_or_create_by(id: item.id) - qbo_item.name = item.name - qbo_item.save! - } + + ids = items.map {|i| i.id} + names = items.map {|i| i.name} + + # Update the invoice table + find_or_create_by(id: ids, name: names) #remove deleted items where.not(items.map(&:id)).destroy_all