Tutorial :Rails - deleting a single record in a intersection table habtm



Question:

I have a habtm relationship (assignments < assignments_candidates > candidates)

I want to be able to delete one candidate off an assignment. here is my code so far

 @assignment = Assignment.find(:first,    :joins => :candidates,    :select => "assignments_candidates.*",    :conditions => ["assignments_candidates.candidate_id = ? AND assignments_candidates.assignment_id = ?",       params[:candidate_id], params[:assignment_id]]    )    @assignment.destroy  

At the moment all i think this does is destroy the object not the record in the intersection table

any ideas ?

Thanks, Alex


Solution:1

Here is how I did it for future reference.

  assignment = Assignment.find(params[:assignment_id])    candidate = assignment.candidates.find(params[:candidate_ids])    assignment.candidates.delete(candidate)  


Solution:2

Have you added a :dependent => :destroy qualifier to the has_many (or has_and_belongs_to_many) relationships the associated models?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »