Removes a record from an query. Does not destroy the passed +record+,
        but marks any joining records for destruction (in the case of a
        many-to-many relationship) or sets the foreign key of +record+ to None
        (in the case of a one-to-many relationship). Only intended to be
        used in conjunction with associations (i.e. do not use if self.record
        is None).

def delete(self, record):
        """
        Removes a record from an query. Does not destroy the passed +record+,
        but marks any joining records for destruction (in the case of a
        many-to-many relationship) or sets the foreign key of +record+ to None
        (in the case of a one-to-many relationship). Only intended to be
        used in conjunction with associations (i.e. do not use if self.record
        is None).
        """
        # note: does (and should) not delete or destroy the record
        if self.record:
            self._validate_record(record)
            if self.join_args:
                # Need to find out who has the foreign key
                # If record has it, set to None, then done.
                # If one level up has it, mark the record for destruction
                final_table = self.join_args[0]['table']
                if final_table in associations.associations_for(self.model):
                    # +record+ does not have the foreign key
                    # Find the record one level up, then mark for destruction
                    related_class = associations.model_from_name(
                        final_table)
                    related_record = related_class.find_by(
                        **self._related_args(record, related_class))
                    # mark the joining record to be destroyed the primary is saved
                    self.record._delete_related_records.append(related_record)
                else:
                    # We have the foreign key
                    # Look up in the foreign key table, bearing in mind that
                    # this is a belongs_to, so the entry will be singular,
                    # whereas the table name is plural (we need to remove the
                    # 's' at the end)
                    key = associations.foreign_keys_for(self.model
                            )[inflector.singularize(final_table)]
                    # Set the foreign key to None to deassociate
                    setattr(record, key, None)
            else:
                setattr(record, foreign_key(record, self.record), None)
                # Ensure that the change is persisted on save
                self.record._related_records.append(record)