Problem with development mode and "cached" array in lib file

I have been charged with resurrecting a rails 2.3.5 app and was told
not to do any tool upgrades.

At the end of our environment.rb we load our products into our
ProductCatalog like so
ProductCatalog.load

This then populates the array in the ProductCatalog module which looks
like so
module ProductCatalog

@@products = []

def ProductCatalog.load_prod_cat
@@products = Product.find(:all, :order => “desc”)
end

def ProductCatalog.get_product(prod_id, prod_vers_id)
@@products.find do |prod|
prod.prod_id.eql?(prod_id) and prod.prod_vers_id.eql?
(prod_vers_id)
end
end
end

calling get product works perfectly and finds the correct product
based on prod_id and prod_vers_id (composit_primary_keys is being used
for this on the model).

The problem arises in the template when we try to iterate over the
array of products we pass in (array is populated via individual calls
to ProductCatalog.get_product) and we try to traverse the
has_many :pricing_structures association.

At this point a method_missing error gets thrown.

This error only occurs in development mode and has me completely
dumbfounded. Our work around is to develop in production mode but
that’s a pain because we have to restart the server everytime which is
a lengthy process due to setup that occurs via calls to a webservice.

Any help would be fantastic

On Sep 22, 4:27pm, kwb [email protected] wrote:

@@products = []

def ProductCatalog.load_prod_cat
@@products = Product.find(:all, :order => “desc”)
end

def ProductCatalog.get_product(prod_id, prod_vers_id)
@@products.find do |prod|
prod.prod_id.eql?(prod_id) and prod.prod_vers_id.eql?
(prod_vers_id)

F**king databases, how do they work?

to ProductCatalog.get_product) and we try to traverse the
has_many :pricing_structures association.

At this point a method_missing error gets thrown.

This error only occurs in development mode and has me completely
dumbfounded. Our work around is to develop in production mode but
that’s a pain because we have to restart the server everytime which is
a lengthy process due to setup that occurs via calls to a webservice.

Any help would be fantastic

Quit? Seriously - this is unbelievably bad code, and I have to believe
that it’s only the tip of the proverbial iceberg.

If that’s not an option, the least you could do is replace the code
with a real database lookup:

def ProductCatalog.load_prod_cat
end

def ProductCatalog.get_product(prod_id, prod_vers_id)
Product.find(prod_id, prod_vers_id)
end

which should (if you’ve got composite_primary_keys set up correctly)
do exactly the same thing without causing problems in development mode
AND without pulling the entire database of products into memory…

The underlying issue (I suspect) is that the array gets cached with
the initial version of the Product class, but subsequent reloads in
development mode redefine the Product constant but the “cached”
version doesn’t change. This tends to cause weird stuff to happen.

–Matt J.