Deleting records in association


#1

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


#2

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


#3

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 .