编码:
user = ... #pretend it has a value :)
instances = Array.new
Product.all.each do |product|
productInstance = ProductInstance.new
productInstance.user = user
productInstance.product = product
instances.push(productInstance)
end
Product
并且ProductInstance
都是 Rails 模型。for-each 循环Product.all
会变慢吗?只是想知道是否有更好的方法来做到这一点......