Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4746 from jenslukowski/issue4718
Test and fix for issue 4718
  • Loading branch information
tenderlove committed Jan 31, 2012
2 parents bb842e8 + e2cea6c commit 5cad7a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -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? &&
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions activerecord/test/cases/identity_map_test.rb
Expand Up @@ -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 #
Expand Down

0 comments on commit 5cad7a0

Please sign in to comment.