Skip to content

Commit 522c0fd

Browse files
committed
The primary key is always initialized in the @attributes hash to nil (unless
another value has been specified).
1 parent 0920065 commit 522c0fd

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

activerecord/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* The primary key is always initialized in the @attributes hash to nil (unless
4+
another value has been specified).
5+
36
* In previous releases, the following would generate a single query with
47
an `OUTER JOIN comments`, rather than two separate queries:
58

activerecord/lib/active_record/attribute_methods/read.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ def cacheable_column?(column)
9292
end
9393

9494
def internal_attribute_access_code(attr_name, cast_code)
95-
if attr_name == primary_key
96-
access_code = "v = @attributes[attr_name];"
97-
else
98-
access_code = "v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) };"
99-
end
95+
access_code = "v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) };"
10096

10197
access_code << "v && #{cast_code};"
10298

activerecord/lib/active_record/core.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def initialize_dup(other)
202202
cloned_attributes.delete(self.class.primary_key)
203203

204204
@attributes = cloned_attributes
205+
@attributes[self.class.primary_key] = nil
205206

206207
run_callbacks(:initialize) if _initialize_callbacks.any?
207208

@@ -326,6 +327,10 @@ def to_ary # :nodoc:
326327
end
327328

328329
def init_internals
330+
pk = self.class.primary_key
331+
332+
@attributes[pk] = nil unless @attributes.key?(pk)
333+
329334
@relation = nil
330335
@aggregation_cache = {}
331336
@association_cache = {}

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def test_joining_has_many_through_belongs_to
738738

739739
def test_select_chosen_fields_only
740740
author = authors(:david)
741-
assert_equal ['body'], author.comments.select('comments.body').first.attributes.keys
741+
assert_equal ['body', 'id'].sort, author.comments.select('comments.body').first.attributes.keys.sort
742742
end
743743

744744
def test_get_has_many_through_belongs_to_ids_with_conditions

activerecord/test/cases/serialization_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def setup
1313
:created_at => Time.utc(2006, 8, 1),
1414
:awesome => false,
1515
:preferences => { :gem => '<strong>ruby</strong>' },
16-
:alternative_id => nil
16+
:alternative_id => nil,
17+
:id => nil
1718
}
1819
end
1920

0 commit comments

Comments
 (0)