I want to introduce a variable in a query, and this generates an error, can someone explain to me what happens?
Here the code:
#Ordenamos el arreglo menor, de menor a mayor
@[email protected]
#Seleccionamos el menor del arreglo menor
@[email protected](1)
#Seleccionamos las muestras de cada periodo, la cantidad de muestras viene dada por el valor de la variable @m
Item.where("periodo=1 AND año='2017'").limit("#{@m}")
@prueba=[]
@consultaPrueba.each do |consulta|
@prueba.push(consulta.ventClient)
end
When executing the query I get the following error:
The error tells you that the value of the variable
@m
is not a number (itlimit
needs a number as a parameter), it is[[3]]
; you get the value as a consequence of usingtake
(which returns an array) and interpolating its value (which converts it to a String ).Consider the following examples:
If you want to take the smallest number from an array you can use
min
:Your code would look like this: