Create tables on the fly in rails?

Hello everyone :slight_smile: Love the mailing list! I read it as much as I can!
Thanks all you RoR guru’s for providing so much help.

Anyways… So say I have Users, and each user can have a list of Plants
that they like. I would want to give each user a table that has many
entries to show me which plants they like. How could I do this?

I don’t know if there’s a way to do it in RoR, so I was just going to
use SQL, and manually create a table corrosponding to the User’s primary
key number. Eg. the first user on my site would have a corrosponding
table favorite_plants_1 and that would hold a list of all the plants he
liked. So I would just do the sql command CREATE TABLE.

Anyone have better advice?

Ben, not to be too picky here, but you pretty much need to learn about
relational databases from the ground up. Just pick one of the Ruby
videos or tutorials and go to town.

You can have one Table for Plants, and another for Users

each user then has_many :plants
Plants belong_to :user

simple


Timothy J.

As Timothy said, or also have a table plants_users and map users to
plants
with a has_and_belongs_to_many

Of course, that would be even better.


Timothy J.

It’s kinda cool

@user = User.find(1)
@plants = @user.plants #makes a collection of the plants that are
related to him by the foreign key


Timothy J.

Timothy J. wrote:

Ben, not to be too picky here, but you pretty much need to learn about
relational databases from the ground up. Just pick one of the Ruby
videos or tutorials and go to town.

You can have one Table for Plants, and another for Users

each user then has_many :plants
Plants belong_to :user

simple


Timothy J.

With those relations, if I grab a user, how do I reference his plants?

Thanks for the quick response - you’re right, Tim, I’ll go grab some
tutorials on this.

Timothy J. wrote:

It’s kinda cool

@user = User.find(1)
@plants = @user.plants #makes a collection of the plants that are
related to him by the foreign key


Timothy J.

Very! Thanks, Tim and Daniel

Ben L. wrote:

Timothy J. wrote:

It’s kinda cool

@user = User.find(1)
@plants = @user.plants #makes a collection of the plants that are
related to him by the foreign key


Timothy J.

Very! Thanks, Tim and Daniel

Correct me if I’m wrong about anything.

First I add the has_many and belongs to in users and plants. Then, in
my migration for plants, I will need to specify a foreign key (being the
user id)?

Like so?
execute “alter table plants add constraint fk_plant_users foreign key
(user_id) references users(id)”

Then, I create the method for a user to add a favorite plant:

def self.add_plant(name, color)
plant = new Plant(name,color)
plants << plant
end

Does the line plants << plant automatically save the new plant in the db
with the foreign key user_id having the value of the primary id key of
the user we are calling this function from?

Or do I have to call user.save after this function? what about
plant.save?
-Ben

I actually thought Ben’s title was pretty interesting. You could, using
the migration files & rake create database tables on the fly I suppose.

I’m sure there’s a better way to actually do it, but you could write a
script that would create a migration file for you based on the
appropriate version, then you could have a CRON job to run rake
db:migrate every hour or something to pick up those changes and create
the tables.

It’s not super elegant, but I suspect it could be done that if you
really needed “on the fly” dynamic tables.

execute "alter table plants add constraint fk_plant_users foreign key

heres a blog post about someone modifying tables on the fly:

http://www.sitharus.com/articles/2006/04/07/dynamic-schemas-in-ruby-on-rails

I actually thought Ben’s title was pretty interesting. You could, using
the migration files & rake create database tables on the fly I suppose.

It’s not super elegant, but I suspect it could be done that if you
really needed “on the fly” dynamic tables.

i suspect you really only need a small handful of tables, for each of
the basic types your SQL store can offer, and can handle the dynamism
elsewhere.

eg. a TextField column, an Integer column, Float, etc. then another
suite of tables for each of the basic AR association types. eg, a ‘tree’
table, an ‘ordered list’ table, etc. each of these having a ‘type’
column to handle the polymorphism.

c