I have a problem related with the HABTM relationships with checkboxes. I''ve got a profile and a service model, both related with has_and_belongs_to_many. The problem is that i can insert new data in the profiles_services table, however, i cant delete already existing data. What could be the problem? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
have you watched... "http://railscasts.com/episodes/17-habtm-checkboxes" tecregio wrote:> I have a problem related with the HABTM relationships with checkboxes. > > I''ve got a profile and a service model, both related with > has_and_belongs_to_many. > > The problem is that i can insert new data in the profiles_services > table, however, i cant delete already existing data. > > What could be the problem?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 9, 12:10 pm, tecregio <francia.fracos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a problem related with the HABTM relationships with checkboxes. > > I''ve got a profile and a service model, both related with > has_and_belongs_to_many. > > The problem is that i can insert new data in the profiles_services > table, however, i cant delete already existing data. > > What could be the problem?Try putting this ProfilesController#update , before #update_attributes is called: params[:profile][:service_ids] ||= [] And put this in ServicesController#update , before #update_attributes is called: params[:service][:profile_ids] || = [] These are needed because forms don''t submit values for unticked checkboxes. If you watch the "HABTM Checkboxes" RailsCast, you''ll learn more about this: http://railscasts.com/episodes/17-habtm-checkboxes Cheers, Nick --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
yes, i think i''m following exactly the tutorial, however it doesn''t work... here''s the code: #profile model: class Profile < ActiveRecord::Base belongs_to :company has_and_belongs_to_many :services [...] end # #service model: class Service < ActiveRecord::Base has_and_belongs_to_many :profiles validates_presence_of :name end # #profile_controller def update params[:profile][:service_ids] ||= [] @profile = Profile.find(params[:id]) @total_services = Service.find(:all, :order => ''name'') @profile.update_attributes(params[:profile]) @company = @profile.company redirect_to :action => ''show'', :id => @company.id end # #partial that displays the services (i''m calling @total_services from the show controller) <table> <% for s in @total_services -%> <tr> <td></td> <td> <%= check_box_tag "profile[service_ids][]", s.id, @profile.services.include?(s) %> <%= s.name -%> </td> </tr> <% end %> </table> # # I''ve checked the generated log, and it creates the insert query when i have newly checked services in my view, but it doesn''t create the delete query when i do uncheck. On 9 dic, 12:14, Andy Koch <pub...-ybbjITnzjhesTnJN9+BGXg@public.gmane.org> wrote:> have you watched... > > "http://railscasts.com/episodes/17-habtm-checkboxes" > > tecregio wrote: > > I have a problem related with the HABTM relationships with checkboxes. > > > I''ve got a profile and a service model, both related with > > has_and_belongs_to_many. > > > The problem is that i can insert new data in the profiles_services > > table, however, i cant delete already existing data. > > > What could be the problem? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hehe silly me, i should have shown you the entire code, i had a service= definition that was conflicting with the deletion =p On 9 dic, 12:25, tecregio <francia.fracos...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> yes, i think i''m following exactly the tutorial, however it doesn''t > work... > > here''s the code: > > #profile model: > > class Profile < ActiveRecord::Base > belongs_to :company > has_and_belongs_to_many :services > [...] > end > # > > #service model: > > class Service < ActiveRecord::Base > has_and_belongs_to_many :profiles > > validates_presence_of :name > > end > > # > > #profile_controller > > def update > params[:profile][:service_ids] ||= [] > @profile = Profile.find(params[:id]) > @total_services = Service.find(:all, :order => ''name'') > @profile.update_attributes(params[:profile]) > @company = @profile.company > redirect_to :action => ''show'', :id => @company.id > end > > # > > #partial that displays the services (i''m calling @total_services from > the show controller) > <table> > > <% for s in @total_services -%> > > <tr> > > <td></td> > > <td> > > <%= check_box_tag "profile[service_ids][]", s.id, > @profile.services.include?(s) %> > > <%= s.name -%> > > </td> > > </tr> > > <% end %> > > </table> > > # > > # > I''ve checked the generated log, and it creates the insert query when i > have newly checked services in my view, but it doesn''t create the > delete query when i do uncheck. > On 9 dic, 12:14, Andy Koch <pub...-ybbjITnzjhesTnJN9+BGXg@public.gmane.org> wrote: > > > have you watched... > > > "http://railscasts.com/episodes/17-habtm-checkboxes" > > > tecregio wrote: > > > I have a problem related with the HABTM relationships with checkboxes. > > > > I''ve got a profile and a service model, both related with > > > has_and_belongs_to_many. > > > > The problem is that i can insert new data in the profiles_services > > > table, however, i cant delete already existing data. > > > > What could be the problem?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---