diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-07-02 06:54:32 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-07-02 06:54:32 -0600 |
commit | b79593f84d0bc601a49e9f7470e862251b7bc145 (patch) | |
tree | 23f057c4f5e7aecc011ed340c1d3544a69827d06 /activerecord/lib | |
parent | 5c87b5c5248154cf8aa76cce9a24a88769de022d (diff) | |
download | rails-b79593f84d0bc601a49e9f7470e862251b7bc145.tar.gz rails-b79593f84d0bc601a49e9f7470e862251b7bc145.tar.bz2 rails-b79593f84d0bc601a49e9f7470e862251b7bc145.zip |
Move pk initialization logic onto `AttributeSet`
Better encapsulates its internals from `ActiveRecord::Base`.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute_set.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 5 |
2 files changed, 7 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb index 8a964fb03c..2889d59328 100644 --- a/activerecord/lib/active_record/attribute_set.rb +++ b/activerecord/lib/active_record/attribute_set.rb @@ -52,6 +52,12 @@ module ActiveRecord super end + def ensure_initialized(key) + unless self[key].initialized? + write_from_database(key, nil) + end + end + protected attr_reader :attributes diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 224112b559..8be5b340de 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -515,10 +515,7 @@ module ActiveRecord end def init_internals - pk = self.class.primary_key - if pk && !@attributes.include?(pk) - @attributes.write_from_database(pk, nil) - end + @attributes.ensure_initialized(self.class.primary_key) @aggregation_cache = {} @association_cache = {} |