Rails - delete a single entry in the habtm intersection table

I have a habtm relation (<assignments_candidates>)

I want to remove one candidate from the job. 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 is that it destroys the object, not the entry in the intersection table

any ideas?

Thanks, Alex

+4
source share
2 answers

Here's how I did it for future reference.

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

Have you added the attribute :dependent => :destroy to the has_many (or has_and_belongs_to_many ) related models?

-1
source

All Articles