diff options
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/primary_key.rb')
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/primary_key.rb | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 7c59664703..7b7811a706 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -1,3 +1,5 @@ +require 'set' + module ActiveRecord module AttributeMethods module PrimaryKey @@ -24,23 +26,30 @@ module ActiveRecord query_attribute(self.class.primary_key) end + # Returns the primary key value before type cast + def id_before_type_cast + read_attribute_before_type_cast(self.class.primary_key) + end + + protected + + def attribute_method?(attr_name) + attr_name == 'id' || super + 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_external_attribute_methods.module_eval <<-CODE, __FILE__, __LINE__ - def id(v, attributes, attributes_cache, attr_name) - attr_name = '#{primary_key}' - send(attr_name, attributes[attr_name], attributes, attributes_cache, attr_name) - end - CODE end end + ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast).to_set + def dangerous_attribute_method?(method_name) - super && !['id', 'id=', 'id?'].include?(method_name) + super && !ID_ATTRIBUTE_METHODS.include?(method_name) end # Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the @@ -96,9 +105,8 @@ module ActiveRecord # end # Project.primary_key # => "foo_id" def primary_key=(value) - @original_primary_key = @primary_key if defined?(@primary_key) - @primary_key = value && value.to_s - @quoted_primary_key = nil + @primary_key = value && value.to_s + @quoted_primary_key = nil end end end |