diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 98aa1ed225b6038033580550e7d6edd0282d60f6..c756924186a79e61baffbcbb98890c3e05f9df22 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -25,6 +25,20 @@ def id? end module ClassMethods + def define_method_attribute(attr_name) + super + + if attr_name == primary_key && attr_name != 'id' + generated_attribute_methods.send(:alias_method, :id, primary_key) + generated_attribute_methods.module_eval <<-CODE, __FILE__, __LINE__ + def self.attribute_id(v, attributes, attributes_cache, attr_name) + attr_name = '#{primary_key}' + send(:'attribute_#{attr_name}', attributes[attr_name], attributes, attributes_cache, attr_name) + end + CODE + end + end + def dangerous_attribute_method?(method_name) super && !['id', 'id=', 'id?'].include?(method_name) end diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb index 41cd194c92fb0ab09b93484884f5348036536708..2fe9968fafe0cc6fb098b13ba26c564e033b0de7 100644 --- a/activerecord/lib/active_record/attribute_methods/read.rb +++ b/activerecord/lib/active_record/attribute_methods/read.rb @@ -58,7 +58,7 @@ def #{attr_name} #{internal} end - def self.attribute_#{attr_name}(v, attributes_cache, attr_name) + def self.attribute_#{attr_name}(v, attributes, attributes_cache, attr_name) #{external} end STR @@ -68,7 +68,7 @@ def self.attribute_#{attr_name}(v, attributes_cache, attr_name) eval(internal) end - singleton_class.send(:define_method, "attribute_#{attr_name}") do |v, attributes_cache, attr_name| + singleton_class.send(:define_method, "attribute_#{attr_name}") do |v, attributes, attributes_cache, attr_name| eval(external) end end @@ -117,8 +117,8 @@ def read_attribute(attr_name) methods = self.class.generated_attribute_methods if methods.respond_to?(accessor) - if @attributes.has_key?(attr_name) - methods.send(accessor, @attributes[attr_name], @attributes_cache, attr_name) + if @attributes.has_key?(attr_name) || attr_name == 'id' + methods.send(accessor, @attributes[attr_name], @attributes, @attributes_cache, attr_name) end elsif !self.class.attribute_methods_generated? # If we haven't generated the caster methods yet, do that and diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index cc8ffb5f271b8f2b8eea01543f6e0214058aec5c..8d248c3f9fb44b63e7be8540f1c1c9723070f5a5 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -23,6 +23,11 @@ def test_to_key_with_customized_primary_key assert_equal keyboard.to_key, [keyboard.id] end + def test_read_attribute_with_custom_primary_key + keyboard = Keyboard.create! + assert_equal keyboard.key_number, keyboard.read_attribute(:id) + end + def test_to_key_with_primary_key_after_destroy topic = Topic.find(1) topic.destroy