More efficient implementation for current_customer_has_subscription? method
Problem
The current_customer_has_subscription?
helper method is used to simply return a boolean for whether a customer has at least one active subscription. However, this method using the following logic, which queries for all subscriptions in Zuora, then subsequently all rate plans and rate plan charges, and initializes Subscription
objects for each one. This is very inefficient, especially for customers with more than one subscription.
def current_customer_has_subscription?
return unless current_customer
SubscriptionsFinder.for_customer(current_customer).present?
end
This seems like an easy win to speed up almost all our page loads because this helper is called in the navigation partial which is used in the application layout (IOW most pages).
Proposal
Instead of using SubscriptionsFinder
, here are a few alternatives:
- A simple fix would be to pull
raw_subscriptions
directly from the customer and return if any are found. This bypasses the queries for rate plans rate plan charges. This could be something like:current_customer.raw_subscriptions.any?
. - Even better would be to limit the query to Zuora to only pull the first subscription. Something like
current_customer.raw_subscriptions(limit: 1).any?
. This would require a few modifications but IronBank supports limits inwhere
(e.g.Zuora::Subscription.where({status: 'Active', account_id: cus.zuora_account_id}, limit: 1)
.
Result
Faster page loads.