Not sure if AR supports query caching for #execute, you might want to dig out the documentation. Anyway, what you can do is use memorization, which means that you save the results manually until the end of the current query.
do something like this in your model:
def repeating_method_with_execute @rs ||= ActiveRecord::Base.execute(...) end
this will basically only start the request for the first time, and then save the response to @rs until the entire request is completed.
If I'm not mistaken, Rails 2.x already has a macro called memoization in ActiveRecord that does all this automatically
hope this helps
Elad meidar
source share