Hello everyone :) 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? -- Posted via http://www.ruby-forum.com/.
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 Johnson On Jun 26, 2006, at 11:48 AM, Ben Lisbakken wrote:> Hello everyone :) 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
As Timothy said, or also have a table plants_users and map users to plants with a has_and_belongs_to_many On 6/27/06, Timothy Johnson <found@foundinteractive.com> 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 Johnson > > > On Jun 26, 2006, at 11:48 AM, Ben Lisbakken wrote: > > > Hello everyone :) 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? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060626/e1f716f6/attachment.html
Of course, that would be even better. -- Timothy Johnson On Jun 26, 2006, at 11:56 AM, Daniel N wrote:> As Timothy said, or also have a table plants_users and map users to > plants > with a has_and_belongs_to_many > > > On 6/27/06, Timothy Johnson <found@foundinteractive.com> 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 Johnson >> >> >> On Jun 26, 2006, at 11:48 AM, Ben Lisbakken wrote: >> >> > Hello everyone :) 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? >> > >> > -- >> > Posted via http://www.ruby-forum.com/. >> > _______________________________________________ >> > Rails mailing list >> > Rails@lists.rubyonrails.org >> > http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Timothy Johnson 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 JohnsonWith 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. -- Posted via http://www.ruby-forum.com/.
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 Johnson On Jun 26, 2006, at 12:01 PM, Ben Lisbakken wrote:> Timothy Johnson 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 Johnson > > 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Timothy Johnson 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 JohnsonVery! Thanks, Tim and Daniel -- Posted via http://www.ruby-forum.com/.
Ben Lisbakken wrote:> Timothy Johnson 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 Johnson > > Very! Thanks, Tim and DanielCorrect 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 -- Posted via http://www.ruby-forum.com/.
> execute "alter table plants add constraint fk_plant_users foreign keyheres 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. 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. -- Posted via http://www.ruby-forum.com/.
> 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