Skip to content
Browse files

Bring back "Reject attributes even if association is loaded" after re…

…basing to master.
  • Loading branch information...
1 parent 08c37b7 commit 333cdcdf5ae42b6c6625f2fd29ae7814ec75d5bf @miloops miloops committed Oct 15, 2010
Showing with 10 additions and 3 deletions.
  1. +10 −3 activerecord/lib/active_record/nested_attributes.rb
View
13 activerecord/lib/active_record/nested_attributes.rb
@@ -399,10 +399,17 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
unless reject_new_record?(association_name, attributes)
association.build(attributes.except(*UNASSIGNABLE_KEYS))
end
+ elsif existing_records.count == 0 #Existing record but not yet associated
+ existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id'])
+ if !call_reject_if(association_name, attributes)
+ association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded?
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
+ end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
- association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes)
- assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
-
+ if !call_reject_if(association_name, attributes)
+ association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded?
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
+ end
else
raise_nested_attributes_record_not_found(association_name, attributes['id'])
end

0 comments on commit 333cdcd

Please sign in to comment.
Something went wrong with that request. Please try again.