Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5662

  • Loading branch information...
commit ea406ddbc161a9d50fe0edf1212ff33bf12256c9 1 parent 6e0c220
@tenderlove tenderlove authored committed
View
8 activerecord/lib/active_record/identity_map.rb
@@ -90,7 +90,7 @@ def get(klass, primary_key)
end
def add(record)
- repository[record.class.symbolized_sti_name][record.id] = record
+ repository[record.class.symbolized_sti_name][record.id] = record if contain_all_columns?(record)
end
def remove(record)
@@ -104,6 +104,12 @@ def remove_by_id(symbolized_sti_name, id)
def clear
repository.clear
end
+
+ private
+
+ def contain_all_columns?(record)
+ (record.class.column_names - record.attribute_names).empty?
+ end
end
# Reinitialize an Identity Map model object from +coder+.
View
25 activerecord/lib/active_record/inheritance.rb
@@ -81,15 +81,7 @@ def instantiate(record, column_types = {})
record_id = sti_class.primary_key && record[sti_class.primary_key]
if ActiveRecord::IdentityMap.enabled? && record_id
- if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
- record_id = record_id.to_i
- end
- if instance = IdentityMap.get(sti_class, record_id)
- instance.reinit_with('attributes' => record)
- else
- instance = sti_class.allocate.init_with('attributes' => record)
- IdentityMap.add(instance)
- end
+ instance = use_identity_map(sti_class, record_id, record)
else
column_types = sti_class.decorate_columns(column_types)
instance = sti_class.allocate.init_with('attributes' => record,
@@ -153,6 +145,21 @@ def compute_type(type_name)
private
+ def use_identity_map(sti_class, record_id, record)
+ if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
+ record_id = record_id.to_i
+ end
+
+ if instance = IdentityMap.get(sti_class, record_id)
+ instance.reinit_with('attributes' => record)
+ else
+ instance = sti_class.allocate.init_with('attributes' => record)
+ IdentityMap.add(instance)
+ end
+
+ instance
+ end
+
def find_sti_class(type_name)
if type_name.blank? || !columns_hash.include?(inheritance_column)
self
View
17 activerecord/test/cases/identity_map_test.rb
@@ -404,18 +404,13 @@ def test_find_using_identity_map_respects_readonly
assert comment.save
end
- def test_find_using_select_and_identity_map
- author_id, author = Author.select('id').first, Author.first
+ def test_do_not_add_to_repository_if_record_does_not_contain_all_columns
+ author = Author.select(:id).first
+ post = author.posts.first
- assert_equal author_id, author
- assert_same author_id, author
- assert_not_nil author.name
-
- post, post_id = Post.first, Post.select('id').first
-
- assert_equal post_id, post
- assert_same post_id, post
- assert_not_nil post.title
+ assert_nothing_raised do
+ assert_not_nil post.author.name
+ end
end
# Currently AR is not allowing changing primary key (see Persistence#update)
Please sign in to comment.
Something went wrong with that request. Please try again.