here it goes... i am creating an online store for tshirts. i have seperate tables for shirts, colors, sizes, and corresponding join tables. in all three of my models i have set up habtm relationships. my view looks like this... [code excerpt] <p> <% for color in @colors%> <input type=''checkbox'' name=''shirt[color_ids][]'' value=''<%= color.id %>'' <%if @shirt.colors.include? color%>checked="checked"<%end%> /> <%= color.name %> <% end %> </p> <p> <% for size in @sizes%> <input type=''checkbox'' name=''shirt[size_ids][]'' value=''<%= size.id %>'' <%if @shirt.sizes.include? size%>checked="checked"<%end%> /> <%= size.name %> <% end %> </p> [/code excerpt] the ''create'' form only inserts info for one of the habtm relationships, but the ''update/edit'' form will insert both. here''s my shirt controller [code excerpt] def new @shirt = Shirt.new @sizes = Size.find_all @colors = Color.find_all end def create @shirt = Shirt.new(params[:shirt]) if @shirt.save flash[:notice] = ''Shirt was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end def edit @shirt = Shirt.find(params[:id]) @sizes = Size.find_all @colors = Color.find_all end def update @shirt = Shirt.find(params[:id]) #added the following 3 lines if !params[''shirt''][''size_ids''] @shirt.sizes.clear end #and these 3 lines if !params[''shirt''][''color_ids''] @shirt.colors.clear end if @shirt.update_attributes(params[:shirt]) flash[:notice] = ''Shirt was successfully updated.'' redirect_to :action => ''show'', :id => @shirt else render :action => ''edit'' end end [/code excerpt] i''m banging my head against the wall trying to figure out this seemingly simple problem. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
ara.t.howard-32lpuo7BZBA@public.gmane.org
2005-Dec-09 03:12 UTC
Re: multiple habtm problem
On Thu, 8 Dec 2005, Anthony Rudgick wrote:> the ''create'' form only inserts info for one of the habtm relationships, but > the ''update/edit'' form will insert both. > > here''s my shirt controller > > [code excerpt] > def new > @shirt = Shirt.new > @sizes = Size.find_all > @colors = Color.find_all > end > > def create > @shirt = Shirt.new(params[:shirt])i think you want to do something like @shirt.colors << Color.find_all @shirt.sizes << Size.find_all here ?? -a -- ==============================================================================| ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ============================================================================== _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
still a no go... i''m stumped. no matter what i change it''s only inserting info from one collection to the join. both will work, but only one at a time on create. works flawlessly in update _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
i''ve established that only the habtm relationhip listed first in my shirt model will submit to the join on the ''new'' action yet both still work on the ''update'' action -a _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I''ve found this too Anthony. Have you opened up a bug report on this? -Nick On 12/9/05, Anthony Rudgick <arudgick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i''ve established that only the habtm relationhip listed first in my shirt > model will submit to the join on the ''new'' action > > yet both still work on the ''update'' action > > -a > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Hey Nick, Anthony, ara, Search the archives of this mailing list for: *[Rails] Problem with .save when model has two HABTM * Several people have contibuted to this ''feature''. The short of it, on create you have to save the collection owner and then alter the collections. Not ideal, but works. It still might be a good idea to submit an issue for this; the documentation suggests even the first collection shouldn''t be saved for newly created models, though. -Mel On 12/15/05, Nick Stuart <nicholas.stuart-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''ve found this too Anthony. Have you opened up a bug report on this? > > -Nick > > On 12/9/05, Anthony Rudgick <arudgick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > i''ve established that only the habtm relationhip listed first in my > shirt > > model will submit to the join on the ''new'' action > > > > yet both still work on the ''update'' action > > > > -a > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I submitted a ticket a couple of days ago. http://dev.rubyonrails.org/ticket/3213 Apparently it has been fixed. I haven''t checked out the latest trunk yet to test it. On 15 Dec 2005, at 20:57, Mel Riffe wrote:> Hey Nick, Anthony, ara, > > Search the archives of this mailing list for: [Rails] Problem > with .save when model has two HABTM > > Several people have contibuted to this ''feature''. > > The short of it, on create you have to save the collection owner > and then alter the collections. > > Not ideal, but works. > > It still might be a good idea to submit an issue for this; the > documentation suggests even the first collection shouldn''t be saved > for newly created models, though. > > -Mel > > > On 12/15/05, Nick Stuart <nicholas.stuart-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > I''ve found this too Anthony. Have you opened up a bug report on this? > > -Nick > > On 12/9/05, Anthony Rudgick <arudgick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > i''ve established that only the habtm relationhip listed first in > my shirt > > model will submit to the join on the ''new'' action > > > > yet both still work on the ''update'' action > > > > -a > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ya, found this ticket and tried out the patch. Seems to work alright for me. On 12/15/05, Steven Mohapi-Banks <steven.mohapibanks-ee4meeAH724@public.gmane.org> wrote:> I submitted a ticket a couple of days ago. > > http://dev.rubyonrails.org/ticket/3213 > > Apparently it has been fixed. I haven''t checked out the latest trunk yet to > test it. > > > On 15 Dec 2005, at 20:57, Mel Riffe wrote: > Hey Nick, Anthony, ara, > > Search the archives of this mailing list for: [Rails] Problem with .save > when model has two HABTM > > Several people have contibuted to this ''feature''. > > The short of it, on create you have to save the collection owner and then > alter the collections. > > Not ideal, but works. > > It still might be a good idea to submit an issue for this; the > documentation suggests even the first collection shouldn''t be saved for > newly created models, though. > > -Mel > > > On 12/15/05, Nick Stuart <nicholas.stuart-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''ve found this too Anthony. Have you opened up a bug report on this? > > > > -Nick > > > > On 12/9/05, Anthony Rudgick <arudgick-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > i''ve established that only the habtm relationhip listed first in my > shirt > > > model will submit to the join on the ''new'' action > > > > > > yet both still work on the ''update'' action > > > > > > -a > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >