Hi, I''m learning rails and have run into what for me is a difficult problem. I have the following associations with a galleries_pictures and a pictures_questions join table: class Picture < ActiveRecord::Base has_and_belongs_to_many :questions has_and_belongs_to_many :galleries class Question < ActiveRecord::Base has_and_belongs_to_many :pictures class Gallery < ActiveRecord::Base has_and_belongs_to_many :pictures In the edit question view, a question and its associated picture(s) can be edited. My problem is, I cannot figure out how to delete the picture in the question and at the same time not delete it from the gallery ie delete the pictures_questions association but not the galleries_pictures association. The action called from the view deletes the row in both join tables: def destroy @picture = Picture.find(params[:id]) @picture.destroy Thanks! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/40e6e1ce0974b1d11da257d0402b29eb%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On Fri, May 31, 2013 at 6:50 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> My problem is, I cannot figure out how to delete the picture > in the question and at the same time not delete it from the gallery ie > delete the pictures_questions association but not the > galleries_pictures association.http://guides.rubyonrails.org/association_basics.html#has_and_belongs_to_many-association-reference -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yDB8TodXqJxcbR1YeqcPSsrZLOnuff%2B96vGi2Dv9%2B1gLg%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Dave Castellano wrote in post #1110796:> Hi, > > I''m learning rails and have run into what for me is a difficult problem. > I have the following associations with a galleries_pictures and a > pictures_questions join table: > > class Picture < ActiveRecord::Base > has_and_belongs_to_many :questions > has_and_belongs_to_many :galleries > > class Question < ActiveRecord::Base > has_and_belongs_to_many :pictures > > class Gallery < ActiveRecord::Base > has_and_belongs_to_many :pictures > > In the edit question view, a question and its associated picture(s) can > be edited. My problem is, I cannot figure out how to delete the picture > in the question and at the same time not delete it from the gallery ie > delete the pictures_questions association but not the > galleries_pictures association. > > The action called from the view deletes the row in both join tables: > def destroy > @picture = Picture.find(params[:id]) > @picture.destroy > > Thanks!Here you are destroying the picture object so naturally Rails will manage the associations automatically. What you want to do instead is to delete the association not the actual picture object. There are a couple of ways to do this. But, you do not wan to use the destroy action on pictures_controller. That action should be used to destroy pictures not associations. Option 1—Expose the join model and manage the association with standard REST destroy action on the association model: class Picture < ActiveRecord::Base has_many :question_pictures has_many :questions, :through => :question_pictures end class Question < ActiveRecord::Base has_many :question_pictures has_many :pictures, :through => :question_pictures end class QuestionPicture < ActiveRecord::Base belongs_to :question belongs_to :picture end question_pictures_controller ----------------- def destroy question_picture = QuestionPicture.find(params[:id]) question_picture.destroy end Option 2—Add an additional RESTful action to questions_controller config/routes.rb ------------------- resources :questions do delete ''remove_picture'', :on => :member end questions_controller.rb ------------------- def remove_picture question = Question.find(:id) picture = question.pictures.find(params[:picture_id]) question.pictures.delete(picture) end Example URL: DELETE: http://example.com/galleries/1/remove_picture/?picture_id=1 P.S. None of this code has been tested. Just wrote it by memory. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/aff8498fa1b785d0249280cff48a07c8%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
> > P.S. None of this code has been tested. Just wrote it by memory.Thank you. I get it. It worked well with a couple of small tweaks. ...wish I could do that from memory :-) Thanks, Dave -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6a2060f9a68d580f25105951cbfbf98b%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.