# Has\_many :through delete(object) only NULL foreign keys?

**URL:** <https://www.ruby-forum.com/t/has-many-through-delete-object-only-null-foreign-keys/117551>\
**Category:** Rails\
**Created:** [October 18, 2007, 12:11am UTC](https://www.ruby-forum.com/t/has-many-through-delete-object-only-null-foreign-keys/117551 "2007-10-18T00:11:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [October 18, 2007, 12:11am UTC](https://www.ruby-forum.com/t/has-many-through-delete-object-only-null-foreign-keys/117551/1 "2007-10-18T00:11:44Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [October 18, 2007, 12:29am UTC](https://www.ruby-forum.com/t/has-many-through-delete-object-only-null-foreign-keys/117551/2 "2007-10-18T00:29:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [October 18, 2007, 12:46am UTC](https://www.ruby-forum.com/t/has-many-through-delete-object-only-null-foreign-keys/117551/3 "2007-10-18T00:46:12Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [October 18, 2007, 10:17am UTC](https://www.ruby-forum.com/t/has-many-through-delete-object-only-null-foreign-keys/117551/4 "2007-10-18T10:17:11Z")

</div>

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