aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder.rb12
-rw-r--r--activerecord/test/cases/relations_test.rb4
3 files changed, 17 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 298ee21e01..f0d9f1e538 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Deprecate passing a class as a value in a query. Users should pass strings
+ instead.
+
+ *Melanie Gilman*
+
* `add_timestamps` and `remove_timestamps` now properly reversible with
options.
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index eb21d01465..67e646bf18 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -35,7 +35,7 @@ module ActiveRecord
# PriceEstimate.where(estimate_of: treasure)
if klass && reflection = klass._reflect_on_association(column)
if reflection.polymorphic? && base_class = polymorphic_base_class_from_value(value)
- queries << self.class.build(table[reflection.foreign_type], base_class)
+ queries << self.class.build(table[reflection.foreign_type], base_class.name)
end
column = reflection.foreign_key
@@ -84,8 +84,7 @@ module ActiveRecord
end
register_handler(BasicObject, ->(attribute, value) { attribute.eq(value) })
- # FIXME: I think we need to deprecate this behavior
- register_handler(Class, ->(attribute, value) { attribute.eq(value.name) })
+ register_handler(Class, ->(attribute, value) { deprecate_class_handler; attribute.eq(value.name) })
register_handler(Base, ->(attribute, value) { attribute.eq(value.id) })
register_handler(Range, ->(attribute, value) { attribute.between(value) })
register_handler(Relation, RelationHandler.new)
@@ -100,6 +99,13 @@ module ActiveRecord
end
private_class_method :handler_for
+ def self.deprecate_class_handler
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ Passing a class as a value in an Active Record query is deprecated and
+ will be removed. Pass a string instead.
+ MSG
+ end
+
protected
attr_reader :klass, :table
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index fc9637a167..3a0398d08d 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -712,7 +712,9 @@ class RelationTest < ActiveRecord::TestCase
def test_find_by_classname
Author.create!(:name => Mary.name)
- assert_equal 1, Author.where(:name => Mary).size
+ assert_deprecated do
+ assert_equal 1, Author.where(:name => Mary).size
+ end
end
def test_find_by_id_with_list_of_ar