From 31a54e49c5c95281df019cfbe1593232886eaeaa Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 9 Apr 2020 14:06:18 +0200 Subject: [PATCH] Allow User#default_card to work with eager-loading --- app/models/spree/user.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 135e54d306..63d51f657c 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -125,7 +125,12 @@ module Spree end def default_card - credit_cards.where(is_default: true).first + # Don't re-fetch associated cards from the DB if they're already eager-loaded + if credit_cards.loaded? + credit_cards.to_a.find(&:is_default) + else + credit_cards.where(is_default: true).first + end end # Checks whether the specified user is a superadmin, with full control of the