[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

CartItem count being 0 or 1 all the time

SpringFlowers AutumnMoon

9/11/2007 4:04:00 AM

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?

2 Answers

SpringFlowers AutumnMoon

9/11/2007 4:31:00 AM

0

[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?


Eugene Bolshakov

9/11/2007 8:36:00 AM

0

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