def assign_nested_attributes(association_name, attributes, options = {})
      attributes = validate_attributes(attributes)
      association_name = association_name.to_sym
      send("#{association_name}=", []) if send(association_name).nil?

      attributes.each_value do |params|
        if params['id'].blank?
          unless reject_new_record?(params, options)
            new = association_name.to_c.new(params.except(*UNASSIGNABLE_KEYS))
            send(association_name).push(new)
          end
        else
          existing = send(association_name).detect { |record| record.id.to_s == params['id'].to_s }
          assign_to_or_mark_for_destruction(existing, params)
        end
      end
      (self.nested_attributes ||= []).push(association_name)
    end
def assign_nested_attributes(association_name, attributes, options = {})
      attributes = validate_attributes(attributes)
      association_name = association_name.to_sym
      send("#{association_name}=", []) if send(association_name).nil?

      attributes.each_value do |params|
        if params['id'].blank?
          unless reject_new_record?(params, options)
            new = association_name.to_c.new(params.except(*UNASSIGNABLE_KEYS))
            send(association_name).push(new)
          end
        else
          existing = send(association_name).detect { |record| record.id.to_s == params['id'].to_s }
          assign_to_or_mark_for_destruction(existing, params)
        end
      end
      (self.nested_attributes ||= []).push(association_name)
    end
Assigns the given nested child attributes.

 Attribute hashes with an +:id+ value matching an existing associated object will update
 that object. Hashes without an +:id+ value will build a new object for the association.
 Hashes with a matching +:id+ value and a +:_destroy+ key set to a truthy value will mark
 the matched object for destruction.

 Pushes a key of the association name onto the parent object's +nested_attributes+ attribute.
 The +nested_attributes+ can be used for determining when the parent has associated children.

 @param [Symbol] association_name The attribute name of the associated children.
 @param [ActiveSupport::HashWithIndifferentAccess, ActionController::Parameters] attributes
     The attributes provided by Rails ActionView. Typically new objects will arrive as
     ActiveSupport::HashWithIndifferentAccess and updates as ActionController::Parameters.
 @param [Hash] options The options to control how nested attributes are applied.

 @option options [Proc, Symbol] :reject_if Allows you to specify a Proc or a Symbol pointing
     to a method that checks whether a record should be built for a certain attribute
     hash. The hash is passed to the supplied Proc or the method and it should return either
     +true+ or +false+. Passing +:all_blank+ instead of a Proc will create a proc
     that will reject a record where all the attributes are blank.

 The following example will update the amount of the ingredient with ID 1, build a new
 associated ingredient with the amount of 45, and mark the associated ingredient
 with ID 2 for destruction.

   assign_nested_attributes(:ingredients, {
     '0' => { id: '1', amount: '123' },
     '1' => { amount: '45' },
     '2' => { id: '2', _destroy: true }
   })