0

I am trying to import a file into excel, but when I try to create a record with related data it shows me the following error:

no implicit conversion of Symbol into Integer  

in this line:

 list.detallelp_attributes = {Articulo: row["Articulo"], Minimo: row["Minimo"], Maximo: row["Maximo"], IdEmpresa: empresa}

Here is my list controller and my method that I use to import:

 has_many :detallelp, class_name: "Deta", foreign_key: "ListaId"
  accepts_nested_attributes_for :detallelp


  def self.import(file,empresa)#importar
    @errors = []
    spreadsheet = open_spreadsheet(file)
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).each do |i|

      row = Hash[[header, spreadsheet.row(i)].transpose]
      list = find_by_id(row["id"]) || new
      list.attributes = {id: row["id"], Lista: row["Nombre"], Tipo: row["Tipo"], FechaIni: row["Fecha inicial"], FechaFin: row["Fecha final"], IdEmpresa: empresa}

      list.detallelp_attributes = {Articulo: row["Articulo"], Minimo: row["Minimo"], Maximo: row["Maximo"], IdEmpresa: empresa}

      if list.save
        # stuff to do on successful save
       else
         list.errors.full_messages.each do |message|
           @errors << "Error fila #{i}, columna #{message}"
         end
       end


    end
    @errors #  <- need to return the @errors array
  end

my method import in list controller:

  def import
    empresa = current_usuario.empresa_id
    @errors = List.import(params[:file], empresa)
    if @errors.present?
      render :errorimportation; #Redirije a dicha vista para mostrar los errores
      return;
    else
      redirect_to listap_path, notice: "listas importadas."
    end
  end

this is my "deta" controller:

  belongs_to :list, class_name:"List", foreign_key: "ListaId"
LuisC
  • 335
  • 1
  • 11

1 Answers1

1

Your relation is has_many. Therefore, accepts_nested_attributes_for expects an array. And you're giving it a hash. That's the source of the error. Give it an array.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • thanks for answer, But how can I give it an array? If I am entering the data by line in the excel being that the first columns corresponds to the registry and the others to the nested resource one by one – LuisC Feb 03 '17 at 00:22
  • @LuisC: "how can I give it an array?" - at the very least, wrap that hash in an array. So it becomes `list.detallelp_attributes = [{Articulo: row["Articulo"], ... }]`. – Sergio Tulentsev Feb 03 '17 at 08:04
  • 1
    thanks for answer my friend, this works. thank you very much – LuisC Feb 03 '17 at 15:19