<h4>&nbsp;</h4>
<%
  # TODO: shouldn't this logic happen in the controller action instead of the template?
  # Actually, maybe we should make render :active_scaffold work in the controller, and not even have a _nested.rhtml?

  # This assumes that the association is included as a column in the active_scaffold_config.columns collection
  associated_columns = []
  associated_columns = params[:associations].split(" ") unless params[:associations].nil?
  unless associated_columns.empty?
    parent_id = params[:id]
    associated_columns.each do | column_name |
      # find the column and the association
      column = active_scaffold_config.columns[column_name]
      association = column.association

      # determine what constraints we need
      if column.through_association?
        @constraints = {
          association.source_reflection.reverse => {
            association.through_reflection.reverse => parent_id
          }
        }
      else
        @constraints = { association.reverse => parent_id }
      end

      # generate the customized label
      @label = as_(:nested_for_model, :nested_model => active_scaffold_config_for(association.klass).label, :parent_model => format_value(@record.to_label))

      begin
        controller = active_scaffold_controller_for(association.klass)
      rescue ActiveScaffold::ControllerNotFound => error
        concat "#{error.class} - #{error.message}"
      else
        concat render(:active_scaffold => controller.controller_path,
          :constraints => @constraints,
          :conditions => association.options[:conditions],
          :label => h(@label),
          :params => {:nested => true, :parent_column => column_name, :parent_model => association.active_record}
        )
      end
    end
  end
%>
