[Ruby] Dumb ActiveRecord question
Laurel Fan
laurel.fan at gmail.com
Fri Jan 5 14:26:57 PST 2007
On 1/5/07, Alex Vollmer <alex.vollmer at gmail.com> wrote:
> 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
I don't understand ActiveRecord that well, but my first thought is
that in development mode model classes are reloaded on every request
-- are you running in development mode? My second thought is that
this looks like you'll have stale data all over the place, but of
course this can't be the final implementation :)
--
Laurel Fan
http://blog.gorgorg.org
More information about the Ruby
mailing list