# How to create tables/models on the fly?

**URL:** <https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359>\
**Category:** Rails\
**Created:** [April 10, 2006, 10:45am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359 "2006-04-10T10:45:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergeypo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@sergeypo](https://www.ruby-forum.com/u/sergeypo)\
**Post date:** [April 10, 2006, 10:45am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359/1 "2006-04-10T10:45:03Z")

</div>

Hi everyone,

I need to create a lot of new db tables during application lifecycle.  
Like 1000 tables, each holding data from a specific device, collecting  
data in real time. Each table may have different columns number, e.g.  
one is:

datetime, temperature, pressure

and another will be:

datetime, voltage, current, power, resistance.

Can anybody advice how to best achive it, to make good use of Rails  
framework capabilities, e.g. to use @device\_table\_name.temperature etc.  
in templates? One possibility I am trying now is Class.new(superclass)  
but maybe there are proven better approaches?

I didn’t tell that I need to create these new classes from web  
interface, so that regular user could add new devices to the solution  
easily.

Thank you!  
Sergey

---

<div class="post-metadata">

**Author:** ![sergeypo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@sergeypo](https://www.ruby-forum.com/u/sergeypo)\
**Post date:** [April 10, 2006, 10:50am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359/2 "2006-04-10T10:50:43Z")

</div>

why would you need seperate tables???

arent all devices just specialized ‘devices’?

could your problem be solved by inheritance and/or aggregations??

On Monday, April 10, 2006, at 10:45 AM, sergey podlesnyi wrote:

> Thank you!  
> Sergey
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Mikkel B.

www.strongside.dk - Football Portal(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![sergeypo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@sergeypo](https://www.ruby-forum.com/u/sergeypo)\
**Post date:** [April 10, 2006, 10:54am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359/3 "2006-04-10T10:54:47Z")

</div>

Mikkel B. wrote:

> why would you need seperate tables???
> 
> arent all devices just specialized ‘devices’?
> 
> could your problem be solved by inheritance and/or aggregations??

Hi Mikkel,

they should be in sepatate tables for many reasons: scalability (imagine  
1000 devices sending their data in real time, we could split database  
across several drives to store tables); compatibility with SCADA  
applications, and they really have different table structure for  
different devices. So simple solutions like having generic table with  
255 columns for data and device ID column won’t work.

Sergey

---

<div class="post-metadata">

**Author:** ![sergeypo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@sergeypo](https://www.ruby-forum.com/u/sergeypo)\
**Post date:** [April 12, 2006, 1:22am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359/4 "2006-04-12T01:22:11Z")

</div>

I agree this would be a useful feature as some projects entail user  
created “templates” or other “class equivalents” that should be mapped  
to ActiveRecord classes. Creation of classes and tables should not  
always require a developer. Does anyone know of any behind the scenes  
rails magic that would prevent the loading model classes from a database  
on the fly?

> Mikkel B. wrote:
> 
> > why would you need seperate tables???
> > 
> > arent all devices just specialized ‘devices’?
> > 
> > could your problem be solved by inheritance and/or aggregations??

---

<div class="post-metadata">

**Author:** ![sergeypo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@sergeypo](https://www.ruby-forum.com/u/sergeypo)\
**Post date:** [April 12, 2006, 10:25am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359/5 "2006-04-12T10:25:09Z")

</div>

Sorenson wrote:

> I agree this would be a useful feature as some projects entail user  
> created “templates” or other “class equivalents” that should be mapped  
> to ActiveRecord classes. Creation of classes and tables should not  
> always require a developer. Does anyone know of any behind the scenes  
> rails magic that would prevent the loading model classes from a database  
> on the fly?

Since my last post, I’ve made workaround: made a fake table

CREATE TABLE factory  
(  
id int unsigned not null auto\_increment primary key  
);

And a class factory.rb with class methods for creating tables and  
accessing data.

These class methods still use Rails’ connection to database, so you can  
just write

#########################  
class Factory \< ActiveRecord::Base

def self.create\_table(table\_name, rtu\_fields)  
begin  
self.connection.create\_table(table\_name) do |tbl|  
tbl.column :t, :datetime  
end  
rtu\_fields.each do |f|  
self.connection.add\_column(table\_name, f.sql\_field\_name,  
:integer)  
end  
rescue  
return nil  
end  
end

def self.get\_last(table\_name, n)  
self.connection.select\_all(“select \* from #{table\_name} LIMIT #{n}”)  
end

end  
##########################  
Thus, in your application you can use calls like

result = Factory(‘new\_table’, @field\_names\_list)  
recordset = Factory(‘existing\_table\_name’, 10)  
puts recordset[‘column)name’]

So that’s not bad!

Sergey

---

<div class="post-metadata">

**Author:** ![sergeypo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@sergeypo](https://www.ruby-forum.com/u/sergeypo)\
**Post date:** [April 13, 2006, 12:14am UTC](https://www.ruby-forum.com/t/how-to-create-tables-models-on-the-fly/57359/6 "2006-04-13T00:14:18Z")

</div>

Not bad at all! I’ll give it a try.

> ##########################  
> Thus, in your application you can use calls like
> 
> result = Factory(‘new\_table’, @field\_names\_list)  
> recordset = Factory(‘existing\_table\_name’, 10)  
> puts recordset[‘column)name’]
> 
> So that’s not bad!
> 
> Sergey
