From 42f1ad68655be08a0ff7e5a261d1ad21d3fc90a5 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 17 May 2011 20:37:11 +0100 Subject: Pass the attribute and option hashes to build_association The build_association method was added as an API for plugins to hook into in 1398db0. This commit restores this API and the ability to override class.new to return a subclass based on a virtual attribute in the attributes hash. --- .../lib/active_record/associations/collection_association.rb | 8 ++++++-- .../lib/active_record/associations/singular_association.rb | 12 ++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 525ac65722..902ad8cb64 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -423,9 +423,13 @@ module ActiveRecord raise NotImplementedError end + def create_scope + scoped.scope_for_create.stringify_keys + end + def build_record(attributes, options) - record = reflection.build_association - record.assign_attributes(scoped.scope_for_create, :without_protection => true) + record = reflection.build_association(attributes, options) + record.assign_attributes(create_scope.except(*record.changed), :without_protection => true) record.assign_attributes(attributes, options) record end diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 877ddf3ee1..44dbe984d4 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -26,18 +26,18 @@ module ActiveRecord end def build(attributes = {}, options = {}) - record = reflection.build_association - record.assign_attributes( - scoped.scope_for_create.except(klass.primary_key), - :without_protection => true - ) - record.assign_attributes(attributes, options) + record = reflection.build_association(attributes, options) + record.assign_attributes(create_scope.except(*record.changed), :without_protection => true) set_new_record(record) record end private + def create_scope + scoped.scope_for_create.stringify_keys.except(klass.primary_key) + end + def find_target scoped.first.tap { |record| set_inverse_instance(record) } end -- cgit v1.2.3