【发布时间】:2016-08-02 08:49:53
【问题描述】:
我正在用多个元素填充一个数组(其中一些是相等的,这就是我想要的)。我的代码是这样的:
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
但是当我去 Rails 控制台并执行 Order.last.products 时,他不会向我显示我保存在控制器上的重复元素。发生什么事了?
【问题讨论】:
标签: ruby-on-rails ruby