From 2eedd1e133f4e6891bb3dffc1f747850c278e17f Mon Sep 17 00:00:00 2001 From: Ankur Agarwal Date: Thu, 20 Sep 2018 14:28:55 +0530 Subject: [PATCH 1/2] For unique occurrences, handle not unique exception --- lib/schedulable/acts_as_schedulable.rb | 31 ++++++++++++++++---------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/schedulable/acts_as_schedulable.rb b/lib/schedulable/acts_as_schedulable.rb index 5cd12a5..2a1e5f3 100644 --- a/lib/schedulable/acts_as_schedulable.rb +++ b/lib/schedulable/acts_as_schedulable.rb @@ -141,20 +141,27 @@ def acts_as_schedulable(name, options = {}) else existing_records = [] end - - if existing_records.any? - # Overwrite existing records - existing_records.each do |existing_record| - if !occurrences_records.update(existing_record.id, date: occurrence.to_datetime) - puts 'An error occurred while saving an existing occurrence record' + + begin + if existing_records.any? + # Overwrite existing records + existing_records.each do |existing_record| + if !occurrences_records.update(existing_record.id, date: occurrence.to_datetime) + puts 'An error occurred while saving an existing occurrence record' + end + end + else + # Create new record + if !occurrences_records.create(date: occurrence.to_datetime) + puts 'An error occurred while creating an occurrence record' end end - else - # Create new record - if !occurrences_records.create(date: occurrence.to_datetime) - puts 'An error occurred while creating an occurrence record' - end - end + rescue ActiveRecord::RecordNotUnique + # Ignore it + # next + rescue Exception => ex + raise ex + end end From 6c6040e4bd02ed8dd3a13afb09d9b5ef3d526bf1 Mon Sep 17 00:00:00 2001 From: Ankur Agarwal Date: Thu, 20 Sep 2018 14:52:17 +0530 Subject: [PATCH 2/2] Update acts_as_schedulable.rb --- lib/schedulable/acts_as_schedulable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/schedulable/acts_as_schedulable.rb b/lib/schedulable/acts_as_schedulable.rb index 2a1e5f3..3e3c392 100644 --- a/lib/schedulable/acts_as_schedulable.rb +++ b/lib/schedulable/acts_as_schedulable.rb @@ -156,7 +156,7 @@ def acts_as_schedulable(name, options = {}) puts 'An error occurred while creating an occurrence record' end end - rescue ActiveRecord::RecordNotUnique + rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordNotFound => ex # Ignore it # next rescue Exception => ex