aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-26 09:36:40 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-26 09:43:35 -0600
commit3bc314e65830dabd7b1c47ad1fa27be5ace0699f (patch)
treea34a12dd12aca74f837f48bdfd125830e1766744 /activerecord/lib/active_record/attribute_methods
parent031588ebe6b449bcba6e3becfaff6978ba10464a (diff)
downloadrails-3bc314e65830dabd7b1c47ad1fa27be5ace0699f.tar.gz
rails-3bc314e65830dabd7b1c47ad1fa27be5ace0699f.tar.bz2
rails-3bc314e65830dabd7b1c47ad1fa27be5ace0699f.zip
Move writing unknown column exception to null attribute
Making this change revealed several subtle bugs related to models with no primary key, and anonymous classes. These have been fixed as well, with regression tests added.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/primary_key.rb7
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb4
2 files changed, 2 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb
index 1c81a5b71b..cadad60ddd 100644
--- a/activerecord/lib/active_record/attribute_methods/primary_key.rb
+++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -83,12 +83,9 @@ module ActiveRecord
end
def get_primary_key(base_name) #:nodoc:
- return 'id' if base_name.blank?
-
- case primary_key_prefix_type
- when :table_name
+ if base_name && primary_key_prefix_type == :table_name
base_name.foreign_key(false)
- when :table_name_with_underscore
+ elsif base_name && primary_key_prefix_type == :table_name_with_underscore
base_name.foreign_key
else
if ActiveRecord::Base != self && table_exists?
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index 94fce2db60..b3c8209a74 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -70,10 +70,6 @@ module ActiveRecord
attr_name = attr_name.to_s
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
- unless has_attribute?(attr_name) || self.class.columns_hash.key?(attr_name)
- raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"
- end
-
if should_type_cast
@attributes.write_from_user(attr_name, value)
else