# Delete / destroy question

**URL:** <https://www.ruby-forum.com/t/delete-destroy-question/131693>\
**Category:** Rails\
**Created:** [March 12, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/delete-destroy-question/131693 "2008-03-12T15:16:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/delete-destroy-question/131693/1 "2008-03-12T15:16:08Z")

</div>

I have a table with 2 columns, employee\_id and job\_id. There is no ‘id’  
column.

I can find a record by using the condition “employee\_id = ? and job\_id =  
?”

but I cant seem to get delete or destroy to work. delete is an undefined  
method, and destroy fails because [id] = null

Is there any way to get this to work, or do I just need to add an ID  
column?

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/delete-destroy-question/131693/2 "2008-03-12T15:43:55Z")

</div>

Bob Br wrote:

> I have a table with 2 columns, employee\_id and job\_id. There is no ‘id’  
> column.

It sounds like this is a join table between your Employee and Job models  
for a has\_and\_belongs\_to\_many relationship.

> but I cant seem to get delete or destroy to work. delete is an undefined  
> method, and destroy fails because [id] = null
> 
> Is there any way to get this to work, or do I just need to add an ID  
> column?

You probably want to remove entries through either the Employee class or  
the Job class (depending on which makes the most sense at the time).  
For example:

If:

job = Job.find(… # some condition  
emp = Employee.find(… # some condition

then either:

job.employees.delete(emp)

or:

emp.jobs.delete(job)

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/delete-destroy-question/131693/3 "2008-03-12T15:55:53Z")

</div>

> It sounds like this is a join table between your Employee and Job models  
> for a has\_and\_belongs\_to\_many relationship.

Not as familiar with the mappings, but that sounds right. Heres what I  
had in mind.

employees - table with X employees  
jobs - table with Y jobs  
exclusions - table of jobs that employees want hidden

For example  
User A wants to see all of the jobs except for 1,2 and 3  
User B wants to see all except 1, 7 and 12.

With the relationship you proposed, can I add and delete records from  
the exclusion table without removing the job or the employee? Using the  
above, User B now wants to see job 1, and User A wants to hide job 9.

I might be misinterpreting the code you wrote, but would that code just  
delete the mapping (exclusion table) or would it delete the emp/job  
record and delete the exclusions along with it?

> If:
> 
> job = Job.find(… # some condition  
> emp = Employee.find(… # some condition
> 
> then either:
> 
> job.employees.delete(emp)
> 
> or:
> 
> emp.jobs.delete(job)

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/delete-destroy-question/131693/4 "2008-03-12T16:39:54Z")

</div>

Excellent! Thanks for the help

Bob

---

<div class="post-metadata">

**Author:** ![syg6](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@syg6](https://www.ruby-forum.com/u/syg6)\
**Post date:** [March 12, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/delete-destroy-question/131693/5 "2008-03-12T16:08:37Z")

</div>

Bob Br wrote:

> With the relationship you proposed, can I add and delete records from  
> the exclusion table without removing the job or the employee? Using the  
> above, User B now wants to see job 1, and User A wants to hide job 9.

That’s right. Treat a\_job.employees and an\_emp.jobs as arrays to add  
and remove from. You can create items as you add them if necessary, but  
removing them will not delete either item, just the association.

Obviously you can change the name of the association as an\_emp.jobs  
doesn’t read right for excluded jobs and Rails will assume a join table  
of employees\_jobs so you’ll have to specify that, too. Have a look at  
the documentation for has\_and\_belongs\_to\_many for full details. Also  
google for this as there are many good tutorials on how to handle these  
relationships.
