# Dynamically create tables and models depending on parent.att

**URL:** <https://www.ruby-forum.com/t/dynamically-create-tables-and-models-depending-on-parent-att/92125>\
**Category:** Rails\
**Created:** [April 16, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/dynamically-create-tables-and-models-depending-on-parent-att/92125 "2007-04-16T12:23:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 16, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/dynamically-create-tables-and-models-depending-on-parent-att/92125/1 "2007-04-16T12:23:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2007, 11:18am UTC](https://www.ruby-forum.com/t/dynamically-create-tables-and-models-depending-on-parent-att/92125/2 "2007-04-23T11:18:54Z")

</div>

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!
