Search code examples
ruby-on-railsrubyruby-on-rails-4roo

no implicit conversion of Symbol into Integer


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"

Solution

  • 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.