class ActionView::Helpers::FormBuilder

def fields_for_with_nested_attributes(association_name, association, options, block)

def fields_for_with_nested_attributes(association_name, association, options, block)
  name = "#{object_name}[#{association_name}_attributes]"
  association = convert_to_model(association)
  if association.respond_to?(:persisted?)
    association = [association] if @object.public_send(association_name).respond_to?(:to_ary)
  elsif !association.respond_to?(:to_ary)
    association = @object.public_send(association_name)
  end
  if association.respond_to?(:to_ary)
    explicit_child_index = options[:child_index]
    output = ActiveSupport::SafeBuffer.new
    association.each do |child|
      if explicit_child_index
        options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call)
      else
        options[:child_index] = nested_child_index(name)
      end
      if content = fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
        output << content
      end
    end
    output
  elsif association
    fields_for_nested_model(name, association, options, block)
  end
end