Has_many :through delete(object) only NULL foreign keys?

I have join model being used with the has_many :through relationship
and am doing the following…

class Job
has_many :views
has_many :candidates_viewd, :through => :views, :source
=> :candidate
end

class Candidate
has_many :views
has_many :jobs_viewd, :through => :views, :source => :job
end

class View
belongs_to :candidate
belongs_to :job
end

def my_method
user = Candidate.find(1)
job = user.jobs_viewed.first
user.jobs_viewed.delete(job)
end

When I was using has_and_belongs_to I could use the .delete(object)
method and the record in the join table would be deleted. Now
has_many :through delete(object) just NULLifies the foreign key.

Is there a simple way around this or am I going to have to write
custom methods to delete the specific join model record?

BTW, for all the raving I’ve heard about has_many :through it seems to
be much more work and less freedom than has_and_belongs_to (I was
using push_with_attributes and that is being deprecated)

bcardarella wrote:

def my_method
user = Candidate.find(1)
job = user.jobs_viewed.first
user.jobs_viewed.delete(job)
end

why not use:

def my_method
user = Candidate.find(1)
user.jobs_viewed.first.destroy
end

That just destroys the actual job. I want to delete the association in
the job model.

The work around I am using is
ViewedJob.find_by_candidate_id_and_job_id(self.id, job.id).destroy

But this is not a very elegant solution… it seems like I am losing
some simple functionality with has_many :through

On Oct 17, 6:28 pm, Anthony R. [email protected]

You need to set :dependant => :destroy on the :views association.
Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs