I'm populating an array with multiple elements (some of then are equals, thats what I want). My code is something like this:
def create
@order = Order.create()
@order.table = Table.find_by(:number => params['table'][0])
@products ||= []
@qtd = []
Product.all.each do |product|
params['order'].each_pair do |ordered|
if(product.id.to_s == ordered.first)
for i in 0..ordered.second[0].to_i
@order.products << product
@order.save
end
end
end
end
binding.pry #here the @order.products is the way I want to
if @order.save
flash[:success] = "Pedido criado com sucesso."
redirect_to tables_path
else
flash[:danger] = "Erro ao criar pedido."
render :new
end
end
But When I go to rails console and do Order.last.products he dosen't show me de duplicated elements like I saved on my controller. Whats happening?