aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorBohdan Pohorilets <bohdan.pohorilets@ilabsolutions.com>2018-02-01 06:36:37 +0200
committerBohdan Pohorilets <bohdan.pohorilets@ilabsolutions.com>2018-09-26 15:31:37 +0300
commit92e024c6abac16cc5a96ec948c381bfa3f0a22df (patch)
treed09697a04b5048082258e0eeb6760e8aeb224b40 /activerecord
parent29c23cc7bda5ffc0fc37f6388105bdf57f5903f0 (diff)
downloadrails-92e024c6abac16cc5a96ec948c381bfa3f0a22df.tar.gz
rails-92e024c6abac16cc5a96ec948c381bfa3f0a22df.tar.bz2
rails-92e024c6abac16cc5a96ec948c381bfa3f0a22df.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/preloader.rb7
-rw-r--r--activerecord/test/cases/associations/eager_test.rb26
2 files changed, 29 insertions, 4 deletions
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"
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index ca902131f4..79b3b4a6ad 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -1618,6 +1618,32 @@ class EagerAssociationTest < ActiveRecord::TestCase
end
end
+ # Associations::Preloader#preloaders_on works with hash-like objects
+ test "preloading works with an object that responds to :to_hash" do
+ CustomHash = Class.new(Hash)
+
+ assert_nothing_raised do
+ Post.preload(CustomHash.new(comments: [{ author: :essays }])).first
+ end
+ end
+
+ # Associations::Preloader#preloaders_on works with string-like objects
+ test "preloading works with an object that responds to :to_str" do
+ CustomString = Class.new(String)
+
+ assert_nothing_raised do
+ Post.preload(CustomString.new("comments")).first
+ end
+ end
+
+ # Associations::Preloader#preloaders_on does not work with ranges
+ test "preloading fails when Range is passed" do
+ exception = assert_raises(ArgumentError) do
+ Post.preload(1..10).first
+ end
+ assert_equal("1..10 was not recognized for preload", exception.message)
+ end
+
private
def find_all_ordered(klass, include = nil)
klass.order("#{klass.table_name}.#{klass.primary_key}").includes(include).to_a