dynamically defined ar classes

Mike Schwab
Tue, 30 Jan 2007 06:01:24 -0000

I''m using at least 20 primary models, and join models for each
combination plus some hierarchical ones.  I am wondering if it would be
possible to define the model classes iteratively, and then reopen the
definitions that I wanted to add code to.

primaries = [:Argument, :Author, :Book, :Category, :Hyperlink, :List,
:Message, :Ufile, :User, :Language, :Word, :Part_of_speech, :Definition,
:School, :Course, :Meeting, :Task, :Question, :Survey, :Datapoint,
:Code]
primaries.each do |primary|
  class primary
    primaries.each { |p| has_many join_tables(primary, p); has_many p,
:through => join_tables(primary, p) }
  end
end

where join_tables is a function that looks into a nested hash to find
the symbol that I''ve chosen to name each table in the database.  I do
not want to use single table inheritance.  I thought that there might be
some trouble if a controller tried to load a file like argument.rb and
couldn''t find it, but couldn''t that be addressed by putting
this do-end
loop at a point that always gets executed, like within the application
controller?  Also, I''d need to pluralize the symbols before passing
them
to has_many; must I convert to_s and then back or can I call pluralize
on the symbol?  Do I need to call downcase?

Lastly: can variables be interpolated into backticks (system calls)?