'Rails ActiveRecord::Associations::Preloader not working

We are using Spree and We are trying to calculate the promotion codes applicable to the order and the sql command below is working too much(45 times). When I tried to joins this user, I can't do that because each promotion rule doesn't has user and we can't joins this users our variable.

Spree::User Exists? (68.8ms)  
SELECT 1 AS one FROM "spree_users" 
INNER JOIN "spree_promotion_rule_users" 
  ON "spree_users"."id" = "spree_promotion_rule_users"."user_id"
WHERE "spree_users"."deleted_at" IS NULL 
AND "spree_promotion_rule_users"."promotion_rule_id" = $1 
AND "spree_users"."id" = $2 
LIMIT $3  [["promotion_rule_id", 1], ["id", 1], ["LIMIT", 1]]

It's not an issue. We just want to optimize performance.

# not working
promotions = Spree::Promotion.active.includes(:promotion_rules, promotion_rules: :user)

How can I use joins or includes method in this case?

EDIT:

Why this code is still generating sql?

promotions = ::Spree::Promotion.active.includes(:promotion_rules)
rules = promotions.flat_map{|p| p.rules}
ActiveRecord::Associations::Preloader.new.preload(
        rules.select { |r| r.is_a? ::Spree::Promotion::Rules::User }, 
        promotion_rule_users: :user
      )


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source