Permalink
Browse files

Merge pull request #4746 from jenslukowski/issue4718

Test and fix for issue 4718
  • Loading branch information...
2 parents bb842e8 + e2cea6c commit 5cad7a0dba652f3e942e58087bc88ba5c5390edc @tenderlove tenderlove committed Jan 31, 2012
@@ -318,7 +318,8 @@ def find_with_ids(*ids)
def find_one(id)
id = id.id if ActiveRecord::Base === id
- if IdentityMap.enabled? && where_values.blank? &&
+ if IdentityMap.enabled? &&
+ (where_values.blank? || contains_only_subclass_constraint?(where_values)) &&
limit_value.blank? && order_values.blank? &&
includes_values.blank? && preload_values.blank? &&
readonly_value.nil? && joins_values.blank? &&
@@ -394,5 +395,11 @@ def find_last
def using_limitable_reflections?(reflections)
reflections.none? { |r| r.collection? }
end
+
+ def contains_only_subclass_constraint?(where_values)
+ where_values.length == 1 &&
+ !where_values[0].left.nil? && where_values[0].left.relation.name == table_name &&
+ !where_values[0].right.nil? && where_values[0].right.length == 1 && where_values[0].right[0] == @klass.name
+ end
end
end
@@ -162,6 +162,14 @@ def test_inherited_with_type_attribute
c2 = Comment.find(c.id)
assert_same(c1, c2)
end
+
+ def test_queries_are_not_executed_when_finding_inherited_class_by_id
+ c = comments(:sub_special_comment)
+ SubSpecialComment.find(c.id)
+ assert_no_queries do
+ SubSpecialComment.find(c.id)
+ end
+ end
##############################################################################
# Tests checking dirty attribute behavior with IM #

0 comments on commit 5cad7a0

Please sign in to comment.