# CartItem count being 0 or 1 all the time

**URL:** <https://www.ruby-forum.com/t/cartitem-count-being-0-or-1-all-the-time/114307>\
**Category:** Ruby\
**Created:** [September 11, 2007, 6:05am UTC](https://www.ruby-forum.com/t/cartitem-count-being-0-or-1-all-the-time/114307 "2007-09-11T06:05:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 6:05am UTC](https://www.ruby-forum.com/t/cartitem-count-being-0-or-1-all-the-time/114307/1 "2007-09-11T06:05:27Z")

</div>

This is for people who read or have read the Agile Web D.  
book by Dave T. (Pragmatic Programmers series like for the PickAx  
book).

So it seems like with the Depot app in Chapter 8, if there are  
thousands of users using the website, there will be thousands or tens  
of thousands of CartItem objects in the server machine’s RAM… and the  
Cart object contains the array that tells which user has which  
CartItem objects…

Now, I tried this to see how many CartItem object there are:

in model/cart\_item.rb, i added the class variable @@count

class CartItem

attr\_reader :product, :quantity@@count = 0def initialize(product)  
@product = product  
@quantity = 1  
@@count += 1enddef self.count  
@@count  
enddef count  
@@count  
end

and in store.rhtml, i add the following to the LEFT column of the page

Number of CartItem \<%= CartItem.count %\>

but I tried adding items after items… the count just won’t go from 1  
to 2, 3, 4, 5, 6 but will just keep on being either 0 or 1. Any  
thoughts as to why?

---

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 6:36am UTC](https://www.ruby-forum.com/t/cartitem-count-being-0-or-1-all-the-time/114307/2 "2007-09-11T06:36:28Z")

</div>

[the code is messed up by the copy and paste before:]

This is for people who read or have read the Agile Web D.  
book by Dave T. (Pragmatic Programmers series like for the PickAx  
book).

So it seems like with the Depot app in Chapter 8, if there are  
thousands of users using the website, there will be thousands or tens  
of thousands of CartItem objects in the server machine’s RAM… and  
the  
Cart object contains the array that tells which user has which  
CartItem objects…

Now, I tried this to see how many CartItem object there are:

in model/cart\_item.rb, i added the class variable @@count

class CartItem

attr\_reader :product, :quantity

@@count = 0

def initialize(product)  
@product = product  
@quantity = 1  
@@count += 1  
end

def self.count  
@@count  
end

def count  
@@count  
end

and in store.rhtml, i add the following to the LEFT column of the page

Number of CartItem \<%= CartItem.count %\>

but I tried adding items after items… the count just won’t go from 1  
to 2, 3, 4, 5, 6 but will just keep on being either 0 or 1. Any  
thoughts as to why?

---

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 10:36am UTC](https://www.ruby-forum.com/t/cartitem-count-being-0-or-1-all-the-time/114307/3 "2007-09-11T10:36:54Z")

</div>

Hi,

> > So it seems like with the Depot app in Chapter 8, if there are  
> > thousands of users using the website, there will be thousands or tens  
> > of thousands of CartItem objects in the server machine’s RAM… and the  
> > Cart object contains the array that tells which user has which  
> > CartItem objects…

The Cart object contains the array of current user’s items only.

> > but I tried adding items after items… the count just won’t go from 1  
> > to 2, 3, 4, 5, 6 but will just keep on being either 0 or 1. Any  
> > thoughts as to why?

That happens because on every request the Cart class is loaded and  
@@count is set to 0. Then Cart@initialize is called (only once) and  
@@count is set to 1.

What do you want to achive? Id you’d like to get the total number of  
items in the current user’s cart, then it’s simply Cart#items.size, if  
you’d like to know the total number of items in all users’ carts then  
you need to store it in somewhere (in the database for example) and  
update on adding/removing the product.

Hope that helps.  
Eugene
