views:

324

answers:

2

I'm developing a system (with Rails 2.3.2, Ruby 1.8.7-p72) that has a sizable reporting component. In order to improve performance, I've created a Report model to archive old reports. The idea is that if a matching report already exists for an arbitrary set of conditions then use it, otherwise generate the report and save the results.

Moreover, I'd like to design the Report model in such a way that only the requested attributes have their corresponding SQL queries run. This all stems from the fact that each attribute takes a long time to compute and I'd rather not generate results that won't be used. I.e. I would like to do something like:

def foo
  @foo ||= read_attribute(:foo)
  if @foo.nil?
    @foo = write_attribute(:foo, (expensive SQL query result))
  end
  @foo 
end

The problem I'm experiencing, however, is that results aren't being properly written out to my database and, as a result, the code is constantly reevaluating the SQL query.

Can anyone tell me why

write_attribute
isn't working? Furthermore, is there a better approach?

A: 

Don't you need to call "save" after doing write_attribute?

BRH
Why was this modded down? Doesn't write_attribute require you to still call save to issue the update?
BRH
+1  A: 

Turns out that what I was doing was fine. The real problem was that the object's "id" lookup was being trumped by a piece of code I had elsewhere. I.e. the actual write was occurring to the database, but with the wrong primary key.

D Carney