Permalink
Browse files

The primary key is always initialized in the @attributes hash to nil …

…(unless

another value has been specified).
  • Loading branch information...
1 parent 0920065 commit 522c0fdb555d54ca68d1acfd1bd8e67fd0040086 @tenderlove tenderlove committed Jan 25, 2012
@@ -1,5 +1,8 @@
## Rails 4.0.0 (unreleased) ##
+* The primary key is always initialized in the @attributes hash to nil (unless
+ another value has been specified).
+
* In previous releases, the following would generate a single query with
an `OUTER JOIN comments`, rather than two separate queries:
@@ -92,11 +92,7 @@ def cacheable_column?(column)
end
def internal_attribute_access_code(attr_name, cast_code)
- if attr_name == primary_key
- access_code = "v = @attributes[attr_name];"
- else
- access_code = "v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) };"
- end
+ access_code = "v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) };"
access_code << "v && #{cast_code};"
@@ -202,6 +202,7 @@ def initialize_dup(other)
cloned_attributes.delete(self.class.primary_key)
@attributes = cloned_attributes
+ @attributes[self.class.primary_key] = nil
run_callbacks(:initialize) if _initialize_callbacks.any?
@@ -326,6 +327,10 @@ def to_ary # :nodoc:
end
def init_internals
+ pk = self.class.primary_key
+
+ @attributes[pk] = nil unless @attributes.key?(pk)
+
@relation = nil
@aggregation_cache = {}
@association_cache = {}
@@ -738,7 +738,7 @@ def test_joining_has_many_through_belongs_to
def test_select_chosen_fields_only
author = authors(:david)
- assert_equal ['body'], author.comments.select('comments.body').first.attributes.keys
+ assert_equal ['body', 'id'].sort, author.comments.select('comments.body').first.attributes.keys.sort
end
def test_get_has_many_through_belongs_to_ids_with_conditions
@@ -13,7 +13,8 @@ def setup
:created_at => Time.utc(2006, 8, 1),
:awesome => false,
:preferences => { :gem => '<strong>ruby</strong>' },
- :alternative_id => nil
+ :alternative_id => nil,
+ :id => nil
}
end

0 comments on commit 522c0fd

Please sign in to comment.