aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authoryui-knk <spiketeika@gmail.com>2015-11-15 23:56:52 +0900
committeryui-knk <spiketeika@gmail.com>2015-11-16 23:15:45 +0900
commit817c1825c15013fd0180762ac5c05a2e024a640d (patch)
treeb27701f790e2c7d2e6fcaff93724025de4fb5557 /activerecord/lib/active_record
parent69f72223e82a4a4f16f16ad7abb36ebfd162a366 (diff)
downloadrails-817c1825c15013fd0180762ac5c05a2e024a640d.tar.gz
rails-817c1825c15013fd0180762ac5c05a2e024a640d.tar.bz2
rails-817c1825c15013fd0180762ac5c05a2e024a640d.zip
Except keys of `build_record`'s argument from `create_scope` in initialize_attributes
If argument of `build_record` has key and value which is same as default value of database, we should also except the key from `create_scope` in `initialize_attributes`. Because at first `build_record` initialize record object with argument of `build_record`, then assign attributes derived from Association's scope. In this case `record.changed` does not include the key, which value is same as default value of database, so we should add the key to except list. Fix #21893.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/association.rb9
1 files changed, 6 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index c7b396f3d4..d64ab64c99 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -163,9 +163,12 @@ module ActiveRecord
@reflection = @owner.class._reflect_on_association(reflection_name)
end
- def initialize_attributes(record) #:nodoc:
+ def initialize_attributes(record, except_from_scope_attributes = nil) #:nodoc:
+ except_from_scope_attributes ||= {}
skip_assign = [reflection.foreign_key, reflection.type].compact
- attributes = create_scope.except(*(record.changed - skip_assign))
+ assigned_keys = record.changed
+ assigned_keys += except_from_scope_attributes.keys.map(&:to_s)
+ attributes = create_scope.except(*(assigned_keys - skip_assign))
record.assign_attributes(attributes)
set_inverse_instance(record)
end
@@ -248,7 +251,7 @@ module ActiveRecord
def build_record(attributes)
reflection.build_association(attributes) do |record|
- initialize_attributes(record)
+ initialize_attributes(record, attributes)
end
end