From e9fb6d04bdfbe3dfc4ba1a3cf442ec5d25764300 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 1 Dec 2011 21:55:59 +0000 Subject: Add test for read_attribute(:id) with non-standard PK. Also make it actually work. It slows down all read_attribute accesses to map 'id' to whatever the PK actually is, inside read_attribute. So instead make sure the necessary methods are defined and that they redirect wherever they need to go. --- .../lib/active_record/attribute_methods/primary_key.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activerecord/lib/active_record/attribute_methods/primary_key.rb') diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 98aa1ed225..c756924186 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 @@ module ActiveRecord 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 -- cgit v1.2.3