I have a users table. I have a assignment table. and I have to log in to see my assignments. i can and I hasve a session id. I can make a new assignment and save my id in the user_id field of the assigment table. I want to share my assignments. so I can create a assignments_users table. this way I can have assignment 1 shared with user 1 and 2. and If I log in as user 1 or 2 I see the assignment. so assignement table has these fields for simplicity id name this is where I have problems thinking. how do get my session id to save into the assignments_users table with the id of the new assignment I just created? old create when saving session id to the assignment table ----------------------------- def create params[:assignment]["user_id"] = session[:userid] @assignment= Assignment.new(params[:assignment]) if @assignment.save flash[:notice] = ''assignment was successfully created.'' redirect_to :action => ''index'' else render :action => ''new'' end end Am I going about this wrong? should I use something else has_many => through? if I put the numbers in the assignments_users everything works fine. I feel not smart. --rambling-- i''ve read most of AWDR, I''m half way through ruby for rails (which has really helped) and googled. so if there something in the books I''m missing point me to it and I''ll read. thanks john
Hi -- On Wed, 26 Jul 2006, John Ivanoff wrote:> I have a users table. I have a assignment table. and I have to log in > to see my assignments. i can and I hasve a session id. > I can make a new assignment and save my id in the user_id field of the > assigment table. > > I want to share my assignments. so I can create a assignments_users > table. this way I can have assignment 1 shared with user 1 and 2. and > If I log in as user 1 or 2 I see the assignment. > > so assignement table has these fields for simplicity > id > name > > this is where I have problems thinking. > how do get my session id to save into the assignments_users table with > the id of the new assignment I just created?Basically, the HABTM association between users and assignments means that every user will have an "assignments" collection, and every assignment will have a "users" collection. To associate a user with an assignment in your code, you would do something like this: @assignment = Assignment.new(params[:assignment]) if @assignment.save @assignment.users << User.find(session[:userid]) # etc. All you have to do is add a user to the assignment''s users collection. The rest will happen automatically (assuming you have HABTM and the assignments_users table set up correctly). David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS (reviewed on Slashdot, 7/12/2006!) http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log dblack@wobblini.net => me
thanks David, that works. I tried something like that but didn''t have it quite right. I never tried assigning after the save. D''oh. I was using this as an example and couldn''t forgure it out. http://wiki.rubyonrails.com/rails/pages/AccessControlListExample thanks again. On 7/27/06, dblack@wobblini.net <dblack@wobblini.net> wrote:> Hi -- > > On Wed, 26 Jul 2006, John Ivanoff wrote: > > > I have a users table. I have a assignment table. and I have to log in > > to see my assignments. i can and I hasve a session id. > > I can make a new assignment and save my id in the user_id field of the > > assigment table. > > > > I want to share my assignments. so I can create a assignments_users > > table. this way I can have assignment 1 shared with user 1 and 2. and > > If I log in as user 1 or 2 I see the assignment. > > > > so assignement table has these fields for simplicity > > id > > name > > > > this is where I have problems thinking. > > how do get my session id to save into the assignments_users table with > > the id of the new assignment I just created? > > Basically, the HABTM association between users and assignments means > that every user will have an "assignments" collection, and every > assignment will have a "users" collection. > > To associate a user with an assignment in your code, you would do > something like this: > > @assignment = Assignment.new(params[:assignment]) > if @assignment.save > @assignment.users << User.find(session[:userid]) > # etc. > > All you have to do is add a user to the assignment''s users collection. > The rest will happen automatically (assuming you have HABTM and the > assignments_users table set up correctly). > > > David > > -- > http://www.rubypowerandlight.com => Ruby/Rails training & consultancy > http://www.manning.com/black => RUBY FOR RAILS (reviewed on > Slashdot, 7/12/2006!) > http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log > dblack@wobblini.net => me > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >