def apply_constraints_to_record(record, options = {})
      options[:allow_autosave] = false if options[:allow_autosave].nil?
      constraints = options[:constraints] || active_scaffold_constraints

      config = record.is_a?(active_scaffold_config.model) ? active_scaffold_config : active_scaffold_config_for(record.class)
      constraints.each do |k, v|
        column = config.columns[k]
        if column&.association
          if column.association.collection?
            record.send(k.to_s).send(:<<, column.association.klass.find(v))
          elsif column.association.polymorphic?
            unless v.is_a?(Array) && v.size == 2
              raise ActiveScaffold::MalformedConstraint, polymorphic_constraint_error(column.association), caller
            end
            record.send("#{k}=", v[0].constantize.find(v[1]))
          else # regular singular association
            record.send("#{k}=", column.association.klass.find(v))

            # setting the belongs_to side of a has_one isn't safe. if the has_one was already
            # specified, rails won't automatically clear out the previous associated record.
            #
            # note that we can't take the extra step to correct this unless we're permitted to
            # run operations where activerecord auto-saves the object.
            reverse = column.association.reverse_association
            if reverse.singular? && !reverse.belongs_to? && options[:allow_autosave]
              record.send(k).send("#{reverse.name}=", record)
            end
          end
        else
          record.send("#{k}=", v)
        end
      end
    end
def apply_constraints_to_record(record, options = {})
      options[:allow_autosave] = false if options[:allow_autosave].nil?
      constraints = options[:constraints] || active_scaffold_constraints

      config = record.is_a?(active_scaffold_config.model) ? active_scaffold_config : active_scaffold_config_for(record.class)
      constraints.each do |k, v|
        column = config.columns[k]
        if column&.association
          if column.association.collection?
            record.send(k.to_s).send(:<<, column.association.klass.find(v))
          elsif column.association.polymorphic?
            unless v.is_a?(Array) && v.size == 2
              raise ActiveScaffold::MalformedConstraint, polymorphic_constraint_error(column.association), caller
            end
            record.send("#{k}=", v[0].constantize.find(v[1]))
          else # regular singular association
            record.send("#{k}=", column.association.klass.find(v))

            # setting the belongs_to side of a has_one isn't safe. if the has_one was already
            # specified, rails won't automatically clear out the previous associated record.
            #
            # note that we can't take the extra step to correct this unless we're permitted to
            # run operations where activerecord auto-saves the object.
            reverse = column.association.reverse_association
            if reverse.singular? && !reverse.belongs_to? && options[:allow_autosave]
              record.send(k).send("#{reverse.name}=", record)
            end
          end
        else
          record.send("#{k}=", v)
        end
      end
    end
Applies constraints to the given record.

 Searches through the known columns for association columns. If the given constraint is an association,
 it assumes that the constraint value is an id. It then does a association.klass.find with the value
 and adds the associated object to the record.

 For some operations ActiveRecord will automatically update the database. That's not always ok.
 If it *is* ok (e.g. you're in a transaction), then set :allow_autosave to true.