23

I am having a slight problem with appending data and then adding it into the array.

Here is my code

@order.orderdesc ||= []

@cart.line_items.each do |item|
 @order.orderdesc += item.quantity +  "x" + item.product.title
end

I only want to add item.quantity and item.product.title. They can be accessed.

Thanks

1 Answer 1

37

If you want to add "stuff" in an array, the += is not made for that. You can use the << operator (append at the end of the array):

@order.orderdesc ||= []

@cart.line_items.each do |item|
 @order.orderdesc << item.quantity +  "x" + item.product.title
end

Or you can use .push():

@order.orderdesc ||= []

@cart.line_items.each do |item|
 @order.orderdesc.push( item.quantity +  "x" + item.product.title )
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.