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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs