How to create tables/models on the fly?


#1

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


#2

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/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Mikkel B.

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


#3

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


#4

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??


#5

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


#6

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