# Update quantity

**URL:** <https://www.ruby-forum.com/t/update-quantity/117842>\
**Category:** Rails\
**Created:** [October 21, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/update-quantity/117842 "2007-10-21T12:42:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 21, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/update-quantity/117842/1 "2007-10-21T12:42:44Z")

</div>

Hi,

I added a field to my cart that allows you to update the quantity of  
the item but when I try to update the quantity I get an error:

NoMethodError in CartController#update  
undefined method `update\_attribute’ for #Array:0x3464698

app/models/cart.rb:33:in `update' app/controllers/cart_controller.rb:53:in`update’  
Request  
Parameters: {“qty\_update”=\>“3”, “id”=\>“2”}

* * *

I have the following tables:

1. carts: id, customer\_id
2. cart\_items: id, product\_id, cart\_id, price, amount, created\_at

The cart class:  
def update(product\_id, qty)  
ci = cart\_items.find\_all\_by\_product\_id(product\_id)  
product = Product.find(product\_id)  
ci.update\_attribute(:amount, qty.to\_i)  
return ci  
end

The cart\_controller:  
def update  
@product = Product.find(params[:id])  
qty\_update = params[:qty\_update]  
if request.post?  
@cart.update(params[:id], qty\_update)  
flash[:notice] = “Updated _#{product.title}_”  
redirect\_to :action =\> “view\_cart”  
end  
end

Would anyone have an idea why updating the quantity doesn’t work?  
And also, besides checking that the new quantity is bigger than 1,  
should I test for anything else?

Thanks in advance,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 21, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/update-quantity/117842/2 "2007-10-21T23:58:33Z")

</div>

cart\_items.find\_all\_by\_product\_id(product\_id)

will certainly return an array, not a cart item object… you might use

cart\_items.find\_all\_by\_product\_id(product\_id).first

bye

On Sun, 2007-10-21 at 03:41 -0700, elle wrote:

> ci = cart\_items.find\_all\_by\_product\_id(product\_id)

–  
Carlos JÃºnior  
removed\_email\_address@domain.invalid  
Boa Noite BH - Os melhores momentos da noite  
+55 31 87635606

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 22, 2007, 6:00am UTC](https://www.ruby-forum.com/t/update-quantity/117842/3 "2007-10-22T06:00:33Z")

</div>

> will certainly return an array, not a cart item object… you might use  
> cart\_items.find\_all\_by\_product\_id(product\_id).first

When I changed that, I now get this error:

NameError in CartController#update  
undefined local variable or method `product’ for #\<CartController:  
0x31795c8\>

app/controllers/cart\_controller.rb:54:in `update’  
Request  
Parameters: {“qty\_update”=\>“4”, “id”=\>“2”}

My cart\_controller.rb is:  
49 def update  
50 @product = Product.find(params[:id])  
51 qty\_update = params[:qty\_update]  
52 if request.post?  
53 @cart.update(params[:id], qty\_update)  
54 flash[:notice] = “Updated _#{product.title}_”  
55 redirect\_to :action =\> “view\_cart”  
56 end  
57 end

Any ideas?

TIA,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [October 22, 2007, 6:02am UTC](https://www.ruby-forum.com/t/update-quantity/117842/4 "2007-10-22T06:02:55Z")

</div>

Actually, changing it from @product to product and then changing this  
line: @cart.update(product, qty\_update) – fixed it 🙂

Thanks.  
Elle
