# Update quantity instead of create new

**URL:** <https://www.ruby-forum.com/t/update-quantity-instead-of-create-new/189905>\
**Category:** Rails\
**Created:** [June 9, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/update-quantity-instead-of-create-new/189905 "2010-06-09T17:50:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [June 9, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/update-quantity-instead-of-create-new/189905/1 "2010-06-09T17:50:59Z")

</div>

I have 3 models: Store, Product and Item

How do i get this test to pass?

it “should update item’s quantity” do  
Item.create(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
Item.create(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
Item.first.quantity.should eql(2)  
end

I have a better picture of the situation on Pastie:  
[http://pastie.org/997850](http://pastie.org/997850)

Cheers.

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [June 9, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/update-quantity-instead-of-create-new/189905/2 "2010-06-09T18:01:15Z")

</div>

Marcelo wrote:

> I have 3 models: Store, Product and Item
> 
> How do i get this test to pass?
> 
> it “should update item’s quantity” do  
> Item.create(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
> Item.create(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
> Item.first.quantity.should eql(2)  
> end
> 
> I have a better picture of the situation on Pastie:  
> [http://pastie.org/997850](http://pastie.org/997850)

> it “should update item’s quantity” do  
> Item.create\_or\_update\_quantity(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
> Item.create\_or\_update\_quantity(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
> Item.first.quantity.should eql(2)  
> end

Then write the missing method.

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [June 9, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/update-quantity-instead-of-create-new/189905/3 "2010-06-09T18:02:52Z")

</div>

Robert W. wrote:

> > it “should update item’s quantity” do  
> > Item.create\_or\_update\_quantity(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
> > Item.create\_or\_update\_quantity(:product\_id =\> 1, :store\_id =\> 1, :quantity =\> 1)  
> > Item.first.quantity.should eql(2)  
> > end
> 
> Then write the missing method.

Ugh! Sorry for the duplicate quoting… didn’t mean to do that.
