# Deleting records in association

**URL:** <https://www.ruby-forum.com/t/deleting-records-in-association/167115>\
**Category:** Rails\
**Created:** [May 4, 2009, 8:10am UTC](https://www.ruby-forum.com/t/deleting-records-in-association/167115 "2009-05-04T08:10:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [May 4, 2009, 8:10am UTC](https://www.ruby-forum.com/t/deleting-records-in-association/167115/1 "2009-05-04T08:10:04Z")

</div>

hi,  
i have 3 models  
1.request  
2.user  
3.requesttargetuser

Requesttargetuser  
belongs\_to :request  
belongs\_to :user

User  
has\_many :requesttargetusers, :dependent=\> :destroy  
has\_many :requests,:through =\> :requesttargetusers

Request  
has\_many :requesttargetusers,:dependent=\> :destroy  
has\_many :users, :through=\>:requesttargetusers

Now when i want to add a user as requesttargetuser for a request

I jus use

req=Request.find(:last)  
targetuser=user.find(:last)

req.users\<\<targetuser

If i want to remove a user from the users list of a request,is there any  
method similar to that of adding?

Thanks

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [May 4, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/deleting-records-in-association/167115/2 "2009-05-04T13:47:03Z")

</div>

You may try:

usertodelete = req.users.find(params[:id]) # Get the user to destroy  
usertodelete.destroy

Charanya N. wrote:

> hi,  
> i have 3 models  
> 1.request  
> 2.user  
> 3.requesttargetuser
> 
> Requesttargetuser  
> belongs\_to :request  
> belongs\_to :user
> 
> User  
> has\_many :requesttargetusers, :dependent=\> :destroy  
> has\_many :requests,:through =\> :requesttargetusers
> 
> Request  
> has\_many :requesttargetusers,:dependent=\> :destroy  
> has\_many :users, :through=\>:requesttargetusers
> 
> Now when i want to add a user as requesttargetuser for a request
> 
> I jus use
> 
> req=Request.find(:last)  
> targetuser=user.find(:last)
> 
> req.users\<\<targetuser
> 
> If i want to remove a user from the users list of a request,is there any  
> method similar to that of adding?
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [May 4, 2009, 2:21pm UTC](https://www.ruby-forum.com/t/deleting-records-in-association/167115/3 "2009-05-04T14:21:21Z")

</div>

Paco G. wrote:

> You may try:
> 
> usertodelete = req.users.find(params[:id]) # Get the user to destroy  
> usertodelete.destroy

I do not want to delete the user .I want to delete the user entry in the  
requesttargetuser table.

I Found out a way to do this

req=Request.find(:last)  
user=User.find(:last)

req.users.delete(user)

Thanks Paco .
