Forum: Ruby on Rails Problem with development mode and "cached" array in lib file

9ed5367a28f94871e15a428712d595b0?d=identicon&s=25 kwb (Guest)
on 2011-10-01 16:34
(Received via mailing list)
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
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2011-10-02 16:20
(Received via mailing list)
On Sep 22, 4:27pm, kwb <kylewb...@gmail.com> 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 Jones
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.