5

I am not sure exactly what I should name this question. I just started server-side programming and I need some help.

All the tutorials I have read so far on RoR deal with creating a pre-defined table and with pre-defined fields (id, name, email, etc etc). They use ActiveRecord as base class and saving to db is handled automatically by superclass.

What I am trying to program is something that allows user-defined table with fields. So think of this way. The web UI will have an empty table, the user will name the table, and add columns (field), and after that, add rows, and then later save it. How would I implement this? I am not asking for details, just an overview of it. As I said, all the tutorials I have read so far deal with pre-defined tables with fields where the ActiveRecord subclass is predefined.

So in a nutshell, I am asking, how to create tables in db on runtime, and add fields to the tables.

Hope I was clear, if not, please let me know and i will try to elaborate a bit more. Thanks.

4
  • 3
    If your're just starting out with server side web programming, how about starting with Hello world? :) It seems from your question that you want to build a web controlled database with whole bunch of meta programming. I suggest take gradual steps towards it. I can somewhat visualize how what you want can be done, just not explain it in "just starting server side programming speak". I mean it in the nicest way. Ya? Commented Nov 12, 2011 at 5:55
  • 1
    Letting casual users be your database designer is a risky, risky business. Commented Nov 12, 2011 at 10:01
  • 1
    What you are trying to do is actually VERY ADVANCED and basically a bad idea even then (but actually quite common for newbies!). Users of a system should be storing data. Having them creating tables is just a bad approach and a bad idea unless perhaps you have a huge amount of dbms experience. The schema creator (you in this case) just have to create flexible data structures that will store whatever you need. Just forget the user creating tables approach. After all would they be creating indexes too? Do they understand unique? Do they understand joins? composite keys? yuch. Commented Nov 12, 2011 at 10:21
  • 0xSina asked something. So please programmers to answer in some way or not. I don't understand comments "bad idea, don't do in that way". I have the same problem as 0xSina and I don't want to allow customer to create database, but he just wants to pay such application. And what to do now? Commented Jan 12, 2017 at 23:10

3 Answers 3

2

Unless you're building a DB administration tool (and even maybe then), allowing the user direct access to the database layer in the way you're suggesting is probably a bad idea. Apart from issues of stability and security, it'll get really slow if your users are creating lots of tables.

For instance, if you wanted to search for a certain value across 100 of your users' tables, you'd have to run 100 separate queries. The site would get exponentially slower the more user tables that were created.

A saner way to do it might be to have a Table model like this

class Table < ActiveRecord::Base
  has_many :fields
  has_many :rows
end

Every table would have fields attached to it, and rows to store the corresponding data (which would be encoded somehow).

However, as @Aditya rightly points out, this is not really beginner stuff!

Sign up to request clarification or add additional context in comments.

1 Comment

This is the "inner platform" anti-pattern. You are creating an "inner" database using an existing database platform, one that will perform poorly and be harder to understand then just creating and modifying actual tables. It would be better for him to dynamically alter the schema of a separate database or schema from the application's, with certain security precautions in place of course. I agree that this is not beginner stuff of course.
1

I agree with previous answers generally speaking. It's not clear from your question why you want to create a table at runtime. It's not really obvious what the advantage of doing this would be. If you are just trying to store data that seems to fit into a table with rows and columns, why not just store it as an array in a field of your user table. If your user is allowed to create many tables, then you could have something like

class User < ActiveRecord::Base
     has_many :tables
  end

and then each table might have a field to store a serialized array. Or you could go with Alex's suggestion - the best choice really depends on what you are going to do with the data, how often it changes, whether you need to search it and so on ...

Comments

1

You can create a database as shown in tutorials which stores name of tables and their columns name those your user want. Then you can have worker (which can be build using Redis and Resque, here is simple Tut on Resque and Redis) and have those worker run migration (write migration with variables and use params to replace them) for you for new table in DB as soon as new entry is made in database. Tell me if you have questions on this.

1 Comment

he's JUST STARTING. "which can be build using Redis and Resque" is nutso.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.