Strip off nested attributes of associations before saving, or type-mismatch errors will occur
550664
def save
      self.attributes.stringify_keys!
      self.attributes.delete('customer')
      self.attributes.delete('product')
      self.attributes.delete('credit_card')
      self.attributes.delete('bank_account')
      self.attributes.delete('paypal_account')

      self.attributes, options = extract_uniqueness_token(attributes)
      self.prefix_options.merge!(options)
      super
    end
1370468
def polymorphic_conditions_for(object_or_type, field, *match)
      match = [:id, :type] if match.blank?
      # Note: {} is equivalent to Hash.new which takes a block, so we must do: ({}) or (Hash.new)
      returning({}) do |conditions|
        conditions.merge!(:"#{field}_id" => object_or_type.id) if object_or_type.is_a?(::ActiveRecord::Base) && match.include?(:id)
        
        if match.include?(:type)
          type = case object_or_type
          when ::Class
            object_or_type.name
          when ::Symbol, ::String
            object_or_type.to_s.singularize.classify
          else # Object - or raise NameError as usual
            object_or_type.class.name
          end
          
          conditions.merge!(:"#{field}_type" => type)
        end
      end
    end
1104497
def apply_augmentations(linter):
    """"""
    augment_visit(linter, TypeChecker.visit_attribute, foreign_key_sets)
    augment_visit(linter, TypeChecker.visit_attribute, foreign_key_ids)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_model_field_display_method)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_style_attribute)
    suppress_message(linter, NameChecker.visit_assignname, 'invalid-name', is_urls_module_valid_constant)

    # supress errors when accessing magical class attributes
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_manager_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_admin_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_model_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_field_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_charfield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_datefield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_decimalfield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_filefield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_imagefield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_ipfield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_slugfield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_foreignkeyfield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_manytomanyfield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_onetoonefield_attribute)
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_form_attribute)

    for parents, attrs in VIEW_ATTRS:
        suppress_message(linter, TypeChecker.visit_attribute, 'no-member', generic_is_view_attribute(parents, attrs))

    # formviews have too many ancestors, there's nothing the user of the library can do about that
    suppress_message(linter, MisdesignChecker.visit_classdef, 'too-many-ancestors',
                     is_class('django.views.generic.edit.FormView'))

    # model forms have no __init__ method anywhere in their bases
    suppress_message(linter, ClassChecker.visit_classdef, 'W0232', is_class('django.forms.models.ModelForm'))

    # Meta
    suppress_message(linter, DocStringChecker.visit_classdef, 'missing-docstring', is_model_meta_subclass)
    pylint_newstyle_classdef_compat(linter, 'old-style-class', is_model_meta_subclass)
    suppress_message(linter, ClassChecker.visit_classdef, 'no-init', is_model_meta_subclass)
    suppress_message(linter, MisdesignChecker.leave_classdef, 'too-few-public-methods', is_model_meta_subclass)
    suppress_message(linter, ClassChecker.visit_attribute, 'protected-access', allow_meta_protected_access)

    # Media
    suppress_message(linter, NameChecker.visit_assignname, 'C0103', is_model_media_valid_attributes)
    suppress_message(linter, DocStringChecker.visit_classdef, 'missing-docstring', is_model_media_subclass)
    pylint_newstyle_classdef_compat(linter, 'old-style-class', is_model_media_subclass)
    suppress_message(linter, ClassChecker.visit_classdef, 'no-init', is_model_media_subclass)
    suppress_message(linter, MisdesignChecker.leave_classdef, 'too-few-public-methods', is_model_media_subclass)

    # Admin
    # Too many public methods (40+/20)
    # TODO: Count public methods of django.contrib.admin.options.ModelAdmin and increase
    # MisdesignChecker.config.max_public_methods to this value to count only user' methods.
    # nb_public_methods = 0
    # for method in node.methods():
    #     if not method.name.startswith('_'):
    #         nb_public_methods += 1
    suppress_message(linter, MisdesignChecker.leave_classdef, 'R0904', is_model_admin_subclass)

    # Tests
    suppress_message(linter, MisdesignChecker.leave_classdef, 'R0904', is_model_test_case_subclass)

    # View
    # Method could be a function (get, post)
    suppress_message(linter, ClassChecker.leave_functiondef, 'no-self-use',
                     is_model_view_subclass_method_shouldnt_be_function)
    # Unused argument 'request' (get, post)
    suppress_message(linter, VariablesChecker.leave_functiondef, 'unused-argument',
                     is_model_view_subclass_unused_argument)
    suppress_message(linter, VariablesChecker.leave_functiondef, 'unused-argument', is_argument_named_request)

    # django-mptt
    suppress_message(linter, DocStringChecker.visit_classdef, 'missing-docstring', is_model_mpttmeta_subclass)
    pylint_newstyle_classdef_compat(linter, 'old-style-class', is_model_mpttmeta_subclass)
    suppress_message(linter, ClassChecker.visit_classdef, 'W0232', is_model_mpttmeta_subclass)
    suppress_message(linter, MisdesignChecker.leave_classdef, 'too-few-public-methods', is_model_mpttmeta_subclass)

    # factory_boy's DjangoModelFactory
    suppress_message(linter, TypeChecker.visit_attribute, 'no-member', is_model_factory)
    suppress_message(linter, ClassChecker.visit_functiondef, 'no-self-argument', is_factory_post_generation_method)

    # ForeignKey and OneToOneField
    # Must update this in a thread safe way to support the parallel option on pylint (-j)
    current_leave_module = VariablesChecker.leave_module
    if current_leave_module.__name__ == 'leave_module':
        # current_leave_module is not wrapped
        # Two threads may hit the next assignment concurrently, but the result is the same
        VariablesChecker.leave_module = wrap(current_leave_module, ignore_import_warnings_for_related_fields)
        # VariablesChecker.leave_module is now wrapped
    # else VariablesChecker.leave_module is already wrapped

    # wsgi.py
    suppress_message(linter, NameChecker.visit_assignname, 'invalid-name', is_wsgi_application)
1289464
def nested_records_changed_for_autosave?
      self.class.reflect_on_all_autosave_associations.any? do |reflection|
        association = association_instance_get(reflection.name)
        association && Array.wrap(association.target).any? { |a| a.changed_for_autosave? }
      end
    end
825520
def nested_records_changed_for_autosave?
        @_nested_records_changed_for_autosave_already_called ||= false
        return false if @_nested_records_changed_for_autosave_already_called
        begin
          @_nested_records_changed_for_autosave_already_called = true
          self.class._reflections.values.any? do |reflection|
            if reflection.options[:autosave]
              association = association_instance_get(reflection.name)
              association && Array.wrap(association.target).any?(&:changed_for_autosave?)
            end
          end
        ensure
          @_nested_records_changed_for_autosave_already_called = false
        end
      end
537911
function(attrs, attrsToRemove) {
    _.forEach(attrs, function(val, key) {
      // shouldRemove is either an object or a boolean
      var shouldRemove = attrsToRemove[key];
      if (_.isUndefined(shouldRemove)) {
        return;
      }

      // Support nested object
      if (_.isPlainObject(val) && !_.isArray(val) && _.isPlainObject(shouldRemove)) {
        return this._removeExpandableAttributes(val, shouldRemove);
      }

      // Make sure attribute is an object
      // Strip all nested properties except for `_id`
      if (_.isPlainObject(attrs[key]) && shouldRemove) {
        attrs[key] = _.pick(attrs[key], ['_id']);
      }
    }.bind(this));
  }
214247
function appendAttrs(action, attrs, _updates, opts) {
  var updates = isTypeUpdates(this) ? this : (_updates || (new Updates()));

  if (attrs && hasKeysOnly(attrs)) {
    appendAttrsKeysOnly(action, attrs, updates, opts);
  } else if (typeof attrs === "object") {
    util.forIn(attrs, function(value, key) {
      if (value !== null && typeof value !== 'undefined')
        appendAttr(action, key, value, updates, opts);
    });
  }
  
  return updates;
}
914118
def reflected_names(obj)
      classes = obj.reflections
      class_names = []
      classes.each do |key, cl|
        if eval(cl.class_name).respond_to?("diffable") \
           and cl.association_class != ActiveRecord::Associations::BelongsToAssociation
          class_names << key
        end
      end
      class_names
    end
529131
def association_valid?(reflection, record, index = nil)
        return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)

        context = validation_context unless [:create, :update].include?(validation_context)

        unless valid = record.valid?(context)
          if reflection.options[:autosave]
            indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveRecord::Base.index_nested_attribute_errors)

            record.errors.group_by_attribute.each { |attribute, errors|
              attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)

              errors.each { |error|
                self.errors.import(
                  error,
                  attribute: attribute
                )
              }
            }
          else
            errors.add(reflection.name)
          end
        end
        valid
      end
480182
def _combine_attr_fast_update(self, attr, typ):
        '''

        '''
        values = dict(getattr(self, attr, {}))
        
        for base in self._class_data.bases:
            vals = dict(getattr(base, attr, {}))
            preserve_attr_data(vals, values)
            values = combine(vals, values)
            
        setattr(self, attr, typ(values))
398170
def destroy_key attribute, key
      raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s)
      new_value = send(attribute)
      new_value.delete(key.to_s)
      send("#{attribute}=", new_value)
      self
    end
1023058
def _dump_model(model, attrs=None):
    

    fields = []

    for field in model._meta.fields:
        fields.append((field.name, str(getattr(model, field.name))))

    if attrs is not None:
        for attr in attrs:
            fields.append((attr, str(getattr(model, attr))))

    for field in model._meta.many_to_many:
        vals = getattr(model, field.name)
        fields.append((field.name, '{val} ({count})'.format(
            val=', '.join(map(str, vals.all())),
            count=vals.count(),
        )))

    print(', '.join(
        '{0}={1}'.format(field, value)
        for field, value in fields
    ))
1176696
def destroy_keys! attribute, *keys
      raise "invalid attribute #{attribute}" unless self.class.column_names.include?(attribute.to_s)
      destroy_keys(attribute, *keys).save
    end
1078014
def save_habtm_association?(assoc)
      @record.class.paper_trail_save_join_tables.include?(assoc.name) ||
        ::PaperTrail.request.enabled_for_model?(assoc.klass)
    end
202019
def reset_attribute!(attr)
      attr = database_field_name(attr)
      attributes[attr] = changed_attributes.delete(attr) if attribute_changed?(attr)
    end
1075001
def override_attributes!(attrs={})
      @column_keys = attrs.keys
      @attrs = HashWithIndifferentAccess.new(Hash[attrs.map { |k, v| [ to_key(k), v ] }])
      @attrs.map { |k, v| define_accessor(k) }
    end
1315069
def versioned_changes
      modified_changes = {}
      raw_changes = self.changes.select {|k,_| versioned_attribute?(k)}.to_hash

      raw_changes.each do |key, (previous, current)|
        if reflection = self.class.reflect_on_association(key.to_sym)
          if reflection.collection?
            previous = previous.map(&:versioned_attributes)
            current  = current.map(&:versioned_attributes)
          else
            previous = previous ? previous.versioned_attributes : {}

            current  = if current && !current.marked_for_destruction?
              current.versioned_attributes
            else
              {}
            end
          end
        end

        # This really shouldn't need to be here,
        # but there is some confusion if we're destroying
        # an associated object in a save callback on the
        # parent object. We can't know that the callback
        # is going to destroy this object on save,
        # so we just have to add the association normally
        # and then filter it out here.
        if previous != current
          modified_changes[key] = [previous, current]
        end
      end

      modified_changes
    end