diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-24 22:15:41 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-24 22:15:41 -0700 |
commit | ec13305b21d7146417b17a2cdf976bbc5cac2189 (patch) | |
tree | 2158278cce048036e960eed45e5c7f4a51c1bf96 /activerecord/lib | |
parent | 23b03baba611b0ef664eec9e9384c14099eb73e9 (diff) | |
download | rails-ec13305b21d7146417b17a2cdf976bbc5cac2189.tar.gz rails-ec13305b21d7146417b17a2cdf976bbc5cac2189.tar.bz2 rails-ec13305b21d7146417b17a2cdf976bbc5cac2189.zip |
stop redifining methods on every call to set_primary_key
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/primary_key.rb | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index b891b2c50e..978cd7fbe3 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -14,11 +14,13 @@ module ActiveRecord # Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the # primary_key_prefix_type setting, though. def primary_key - reset_primary_key + @primary_key ||= reset_primary_key end def reset_primary_key #:nodoc: - key = get_primary_key(base_class.name) + key = self == base_class ? get_primary_key(base_class.name) : + base_class.primary_key + set_primary_key(key) key end @@ -40,6 +42,9 @@ module ActiveRecord end end + attr_accessor :original_primary_key + attr_writer :primary_key + # Sets the name of the primary key column to use to the given value, # or (if the value is nil or false) to the value returned by the given # block. @@ -48,9 +53,11 @@ module ActiveRecord # set_primary_key "sysid" # end def set_primary_key(value = nil, &block) - define_attr_method :primary_key, value, &block + @primary_key ||= '' + self.original_primary_key = @primary_key + value &&= value.to_s + self.primary_key = block_given? ? instance_eval(&block) : value end - alias :primary_key= :set_primary_key end end end |