Re: dynamically defined ar classes

Mike Schwab
Tue, 30 Jan 2007 19:58:49 -0000

I''ve found as my application has grown that adding primary models has
been so intimidating that it has stifled my progress.  I started with
the first nine and then added a language module, a schoolwork module,
and plans for a stats module.  But I couldn''t settle on whether to
create join tables for each combination because it would mean I''d have
three hundred more secondary models to keep straight.  Most of them
don''t have much in the way of unique behavior.

If this scheme worked out, I could add a primary model just by adding a
symbol to the array primaries, and adding an element to the join_tables
hash with twenty symbols containing names for join tables.  Then I would
reopen the models to my heart''s content, and if I wanted to add similar
methods to several I could use the same iteration idiom, even if all I
was doing was mixing in a module.

Using magic models might help; thanks for the suggestion.  But I still
think the question should be fair game.  There''s one more wrinkle.  I
might subclass the models like so:
class Primary < ActiveRecord::Base
end

then each actual primary model would descend from Primary
primaries.each do |primary|
  class primary < Primary
  end
end

So: can this work?