Edvard Majakari
2007-May-02 11:07 UTC
Problem loading associated instances after deserialization
Hi, I''m not sure if this is an active record bug, but the problem is as follows: I''m trying to create a cache of results for an expensive computation. I''m storing results to database. Results contain child objects through has_many association. Storing data to cache seems to work fine, as well as reading data SEEMS to work if I use object.inspect method, but when I try to read child objects of any item found in the cache it doesn''t work (it always returns an empty array). A contrived example of what happens: class MyMeasurementCache # each item contains multiple measurements # each measurement has_many results serialize :items_measurements def MyMeasurementCache.run_measurements(items) ... cache = find_existing_or_create_new(compute_digest(items)) if cache.hit? items.zip(cache.items_measurements).each do |item, measurements| item.measurements = measurements # now if I inspect item.measurements, I see results in inspect: # item.measurements.inspect # <MeasurementTypeFoo:0x14c034d8 @attributes={"item_id"=>nil, # @results=[#<Result:0x14c03744 @attributes={...}...] # however, the following always returns an empty array: item.measurements.each do |meas| meas.results # always empty end end else # not found from cache, store measurements cache.items_measurements = items.map { |i| measure_item(i) } ... end end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---