Forum: Ruby on Rails Dynamically create tables and models depending on parent.att

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
819c93493548c6d70becee01363b4bfd?d=identicon&s=25 eric.duminil@gmail.com (Guest)
on 2007-04-16 12:23
(Received via mailing list)
Hi everybody!

Given those models:

###############################################################################
class Company < ActiveRecord::Base
  has_many :employees

class Employee < ActiveRecord::Base
  belongs_to :company

class Attribute < ActiveRecord::Base
  has_many :companies,  :through => :employee_attributes
  has_many :employee_attributes

class EmployeeAttribute < ActiveRecord::Base
  belongs_to :company
  belongs_to :attribute
###############################################################################

is it possible to create a new "#{company.name.underscore}_employees"
table for each company, whose columns are given by
company.employee_attributes?

For example, with:

  Company.find_by_name("GoodCompany").employee_attributes.collect{|e_a|
e_a.name}
  => ["name","position","salary"]

  Company.find_by_name("EvilCompany").employee_attributes.collect{|e_a|
e_a.name}
  => ["name","position","salary","life_expectancy",
"should_be_fired_at"]

,invoking Company::build_employees_tables would create two tables:

  a good_company_employees table with those columns:
    id, name, position, salary
  and an evil_company_employees table with those columns:
    id, name, position, salary, life_expectancy, should_be_fired_at

  and two corresponding models:

    class GoodCompanyEmployee < Employee
    class EvilCompanyEmployee < Employee


Then, Company.find_by_name("GoodCompany").employees and
Company.find_by_name("EvilCompany").employees would ask two different
tables and return
  GoodCompanyEmployee.find :all
and
  EvilCompanyEmployee.find :all
respectively.

Is this even possible?
With my limited Rails experience, I only came up with:
-using STI and leaving life_expectancy and should_be_fired_at columns
as NULL for GoodCompanyEmployee. But in case of a lot of different
companies with a lot of different attributes, employees table would be
99% NULL.
-using serialize :attributes and method_missing to access the
attributes stored in a hash. I suppose this would be really slow and
unDRY.
-using something like magic_multi_connections...
-maybe the whole thing is defective by design, and I could come up
with the same behaviour using a completely different approach?

Any help would be greatly appreciated,
Have a good day (and thanks for the wonderful Framework),

Eric DUMINIL
819c93493548c6d70becee01363b4bfd?d=identicon&s=25 Eric DUMINIL (Guest)
on 2007-04-23 11:18
(Received via mailing list)
up?

Maybe my description was a bit too long, but I just wanted to explain
my problem right and give the reader a few more hints.

Any help would be greatly appreciated, and I promise  I won't bother
you anymore after this one!
This topic is locked and can not be replied to.