Forum: Ruby on Rails Problem loading associated instances after deserialization

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D8fd9cfd2cb2e458ebbaedd2fa3cf30b?d=identicon&s=25 Edvard Majakari (edvardm)
on 2007-05-02 13:07

I'm not sure if this is an active record bug, but the problem is as
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? 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
      # not found from cache, store measurements
      cache.items_measurements = { |i| measure_item(i) }
This topic is locked and can not be replied to.