Skip to content

Instantly share code, notes, and snippets.

@arbarlow
Created May 26, 2011 14:55
Show Gist options
  • Save arbarlow/993308 to your computer and use it in GitHub Desktop.
Save arbarlow/993308 to your computer and use it in GitHub Desktop.
# This is the wrong was to do this. What if one of the order products fails?
def create
@order = Order.new
if @order.save
for order in params[:order][:product_ids]
OrderProducts.create(:order_id => @order.id, :product_id => Product.find(params[:id]).id)
end
flash[:notice] = "Thanks, your order will be completed shortly!"
redirect_to :action => "index"
else
render :action => "new"
end
end
# On save, this will save and validate all the order_products too
def create
@order = Order.new
for order in params[:order][:product_ids]
@order.order_products.build(:product => Product.find(params[:id]))
end
if @order.save
flash[:notice] = "Thanks, your order will be completed shortly!"
redirect_to :action => "index"
else
render :action => "new"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment