Forum: Ruby on Rails How to create tables/models on the fly?

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.
451c775e5b876624750923a403850b0b?d=identicon&s=25 Sergey Podlesnyi (sergeypo)
on 2006-04-10 10:45
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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-04-10 10:50
(Received via mailing list)
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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


Mikkel Bruun

www.strongside.dk    - Football Portal(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
451c775e5b876624750923a403850b0b?d=identicon&s=25 Sergey Podlesnyi (sergeypo)
on 2006-04-10 10:54
Mikkel Bruun 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
24c13792ed78125deef81ab752625a7a?d=identicon&s=25 Sorenson (Guest)
on 2006-04-12 01:22
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 Bruun wrote:
>> why would you need seperate tables???
>>
>> arent all devices just specialized 'devices'?
>>
>> could your problem be solved by inheritance and/or aggregations??
>>
>
451c775e5b876624750923a403850b0b?d=identicon&s=25 Sergey Podlesnyi (sergeypo)
on 2006-04-12 10:25
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
24c13792ed78125deef81ab752625a7a?d=identicon&s=25 Sorenson (Guest)
on 2006-04-13 00:14
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
This topic is locked and can not be replied to.