diff options
5 files changed, 12 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index a458f9e6e4..09431e38f5 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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: diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 3549cbb090..c129dc8c52 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -92,11 +92,7 @@ module ActiveRecord 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};" diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index adba3f710c..a774af6024 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -202,6 +202,7 @@ module ActiveRecord 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 @@ module ActiveRecord end def init_internals + pk = self.class.primary_key + + @attributes[pk] = nil unless @attributes.key?(pk) + @relation = nil @aggregation_cache = {} @association_cache = {} diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 4612bc2618..9cc09194dc 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -738,7 +738,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb index 61b04b3e37..31ba99a8ff 100644 --- a/activerecord/test/cases/serialization_test.rb +++ b/activerecord/test/cases/serialization_test.rb @@ -13,7 +13,8 @@ class SerializationTest < ActiveRecord::TestCase :created_at => Time.utc(2006, 8, 1), :awesome => false, :preferences => { :gem => '<strong>ruby</strong>' }, - :alternative_id => nil + :alternative_id => nil, + :id => nil } end |