SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    creating tables dynamically for each user in Rails

    Hi,

    I have an app in which i want to create 4-5 tables for each user who signs up using his user id. I want to name his tables using his user id so that querying becomes faster.

    how can i do this in rails?

  2. #2
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use methods from ActiveRecord::Migration in your controller. E.g. to create a table you use ActiveRecord::Migration.create_table.

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great.. that works well.. but how do i set up the models and associations so as to keep it DRY?

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am fairly sure what you are doing will not result in efficient use of your database. It certainly isn't good programming practice.

    The whole point of object orientated programming is that you reuse the same coding over and over. Creating a new set of models for each user would very much go against this philosophy. How are you going to manage all these models - for example if you need to modify the way one of the tables works!

    What you should do is have all users using the same 4-5 tables and then uniquely identify the table entries for each user via a user_id field.

    In raw SQL you'd then add a WHERE condition to your SELECT statements. So if the user has id 3 you'd add "WHERE user_id = 3". The database will then return just the entries for that user.

    SQL is designed to retrieve data this way and is very efficient at it.

    ActiveRecord in Rails make this even easier for you by automatically generating the correct WHERE statement for you when you define your has_many, has_one and belongs_to relationships.

  5. #5
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand that.. so this is actually a handicap specific to Rails? Because of the mapping it does between the tables and the models and controllers automatically?

  6. #6
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As the others said, creating 4 or 5 new tables for every new user is a really bad idea, so it isn't a handicap, it's just making what's right easy and what's wrong hard.

    Tie these 4 or 5 models to your User model with belongs_to/has_many relationships. With the right indexes, they will actually work faster than thousands of small tables.

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem isn't Rails. The problem is you are trying to do something that can be done much more easily another way.

    Quote Originally Posted by yourstruly_ram View Post
    I understand that.. so this is actually a handicap specific to Rails?
    No. You can make your models work any way you want. The limit is how well you can program in Ruby. It just the automatic stuff that won't work as well, and the problems you'll get because you'll lose a lot of the simplicity of Rails.

    If you want to overwrite the user model find method, or create customer methods to grab the data you want - Rails has all the tools you need to do that. There is nothing to stop you using custom SQL calls within a model.

    If you want to have a separate set of tables to for each user, the tools are available within Rails to do it. However, it's a silly thing to do and Rails won't do it automatically for you. You'll need to customize your models to suit the crazy plan.

    It's not a handicap of Rails. It just compliance with good programming practice.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •