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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones
2011-Oct-02 14:19 UTC
Re: Problem with development mode and "cached" array in lib file
On Sep 22, 4:27 pm, kwb <kylewb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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)F**king databases, how do they work?> 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 fantasticQuit? 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.