From 92e024c6abac16cc5a96ec948c381bfa3f0a22df Mon Sep 17 00:00:00 2001 From: Bohdan Pohorilets Date: Thu, 1 Feb 2018 06:36:37 +0200 Subject: If association is a hash-like object preloading fails If you pass a hash-like object to preload associations (for example ActionController::Parameters) preloader will fail with the ArgumentError. This change allows passing objects that may be converted to a Hash or String into a preloader --- activerecord/lib/active_record/associations/preloader.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index d4d1b2a282..a8f94b574d 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -98,12 +98,11 @@ module ActiveRecord # Loads all the given data into +records+ for the +association+. def preloaders_on(association, records, scope, polymorphic_parent = false) - case association - when Hash + if association.respond_to?(:to_hash) preloaders_for_hash(association, records, scope, polymorphic_parent) - when Symbol + elsif association.is_a?(Symbol) preloaders_for_one(association, records, scope, polymorphic_parent) - when String + elsif association.respond_to?(:to_str) preloaders_for_one(association.to_sym, records, scope, polymorphic_parent) else raise ArgumentError, "#{association.inspect} was not recognized for preload" -- cgit v1.2.3