CartItem count being 0 or 1 all the time

S

Summercool

This is for people who read or have read the Agile Web Development
book by Dave Thomas (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?
 
S

Summercool

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

This is for people who read or have read the Agile Web Development
book by Dave Thomas (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?
 
E

Eugene Bolshakov

Hi,

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top