Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - deleting a single record in a intersection table habtm

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

like image 752
Alex Avatar asked Dec 16 '22 23:12

Alex


1 Answers

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)
like image 200
Alex Avatar answered Dec 29 '22 10:12

Alex