[Ruby] Dumb ActiveRecord question
Alex Vollmer
alex.vollmer at gmail.com
Fri Jan 5 14:06:09 PST 2007
Get yer rotten veggies ready for this one...
I have a bunch of lookup data in a couple of tables that gets referenced
over and over and over. When I include these lookup tables in a
has_manyassociation with an
:include declaration, ActiveRecord creates some LEFT OUTER JOIN SQL that
performs a sequential table scan.
So insted of eagerly loading the objects through associations I thought I
would hand-roll a class-level method to capture find_by_id queries and cache
them in a hash defined in a class constant. Unfortunately it looks like each
time I run the request the cache is brand-new. Each table is less than 100
rows so holding these records in memory shouldn't have a big effect.
The class looks like this:
class Property < ActiveRecord::Base
self::ID_CACHE = {}
def self.find_by_id(id)
self::ID_CACHE[id] ||= super
end
end
So I'm either user ActiveRecord incorrectly (which is possible) or I don't
understand classes and objects in Ruby so well (also equally likely). Can
anyone shed a little light on this for me?
Thanks!
Alex The Newb
--
"I think there are only three things America will be known for 2,000 years
from now when they study this civilization: the Constitution, jazz music,
and baseball."
--Gerald Early
More information about the Ruby
mailing list