diff options
author | Brendan Deere <brendan@stembolt.com> | 2017-01-09 17:35:40 -0800 |
---|---|---|
committer | Brendan Deere <brendan@stembolt.com> | 2017-01-09 17:35:40 -0800 |
commit | 3878f31f58c4ca9286ccf2dbd4597005dec285fa (patch) | |
tree | 450b42277fceb59a116bcb61f67fc5705ffbb477 /lib/solidus_subscriptions | |
parent | 0dd5bdd17ed0cd67f5395c90aa66c614cc3906d7 (diff) |
Remove some n+1 queries from processing
The processor looks up a number of users, subscriptions and installments
and passes them off to an Active Job.
Before we were doing a number of n+1 queries and relying on the rails
global id when passing objects to the consolidated installments.
This commit removes the greedy queries and passes a list of ids to the
active job object so that all affected objects can be looked up in a
single query
Diffstat (limited to 'lib/solidus_subscriptions')
-rw-r--r-- | lib/solidus_subscriptions/processor.rb | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/lib/solidus_subscriptions/processor.rb b/lib/solidus_subscriptions/processor.rb index 40ff35e..cae2ad6 100644 --- a/lib/solidus_subscriptions/processor.rb +++ b/lib/solidus_subscriptions/processor.rb @@ -55,7 +55,7 @@ module SolidusSubscriptions # Create `ProcessInstallmentsJob`s for the users used to initalize the # instance def build_jobs - users.map { |user| ProcessInstallmentsJob.perform_later installments(user) } + users.map { |user| ProcessInstallmentsJob.perform_later installments(user).map(&:id) } end private @@ -63,6 +63,7 @@ module SolidusSubscriptions def subscriptions_by_id @subscriptions_by_id ||= Subscription. actionable. + includes(:line_item, :user). where(user_id: user_ids). group_by(&:user_id) end @@ -80,8 +81,8 @@ module SolidusSubscriptions end def new_installments(user) - subscriptions_by_id.fetch(user.id, []).map do |sub| - ActiveRecord::Base.transaction do + ActiveRecord::Base.transaction do + subscriptions_by_id.fetch(user.id, []).map do |sub| sub.successive_skip_count = 0 sub.advance_actionable_date sub.cancel! if sub.pending_cancellation? |