diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-26 14:50:18 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-26 14:50:18 -0300 |
commit | f437f1104687e5bb26bf5ce791aee7d9cc0ec62a (patch) | |
tree | cc422be64e95027514920850809a0d1ef788d30e /activerecord/lib | |
parent | 8b0753b4f0d0f66b73aac4cd26d6b575edc99fb6 (diff) | |
parent | 3bc314e65830dabd7b1c47ad1fa27be5ace0699f (diff) | |
download | rails-f437f1104687e5bb26bf5ce791aee7d9cc0ec62a.tar.gz rails-f437f1104687e5bb26bf5ce791aee7d9cc0ec62a.tar.bz2 rails-f437f1104687e5bb26bf5ce791aee7d9cc0ec62a.zip |
Merge pull request #15924 from sgrif/sg-write-unknown-column
Move writing unknown column exception to null attribute
Diffstat (limited to 'activerecord/lib')
5 files changed, 9 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 33c20bb5cc..6d38224830 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -90,6 +90,11 @@ module ActiveRecord def value nil end + + def with_value_from_database(value) + raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{name}`" + end + alias_method :with_value_from_user, :with_value_from_database end class Uninitialized < Attribute # :nodoc: 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 diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 3ef8878ad1..def04dbed2 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -381,7 +381,7 @@ module ActiveRecord end def table_exists?(name) - return false unless name + return false unless name.present? return true if tables(nil, nil, name).any? name = name.to_s diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index c434b0d40a..3321e268d5 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -521,7 +521,7 @@ module ActiveRecord def init_internals pk = self.class.primary_key - unless @attributes.include?(pk) + if pk && !@attributes.include?(pk) @attributes.write_from_database(pk, nil) end |