[Ruby] Dumb ActiveRecord question
Chris Carter
cdcarter at gmail.com
Fri Jan 5 14:32:51 PST 2007
On 1/5/07, Alex Vollmer <alex.vollmer at gmail.com> wrote:
> 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
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>
The way rails makes requests (it does it weirdly, because it is not
threadsafe) reloads the models....kind of, every request. At least it
did, in mongrel, it may have changed. Being in development mode would
also do that.
--
Chris Carter
concentrationstudios.com
brynmawrcs.com
More information about the Ruby
mailing list