# How to delete a record inside of an association

**URL:** <https://www.ruby-forum.com/t/how-to-delete-a-record-inside-of-an-association/163489>\
**Category:** Rails\
**Created:** [March 20, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-inside-of-an-association/163489 "2009-03-20T21:34:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e95f7d/32.png) [@nate](https://www.ruby-forum.com/u/nate)\
**Post date:** [March 20, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-inside-of-an-association/163489/1 "2009-03-20T21:34:03Z")

</div>

I’m a noob having a hard time, and it’s probably really simple.

Here’s the way it’s set up:  
Client has many Bins  
Bin belongs to Client  
In my Client show view I have it render a partial that lists all the  
records in Bin associated with Client.

I need to have a destroy link for each of the Bin records. Right now I  
have it configured in my Bin partial as:  
\<%= link\_to(“Delete”, { :action =\> “destroy”, :id =\> @bin }, :confirm  
=\> “Are you sure?”, :method =\> :delete) %\>

This just throws back an error saying that there is not a Client with  
that id.

I can’t figure out how to tell it to delete the bin not the client  
with that id.

Any helpful suggestions? Cheers.

---

<div class="post-metadata">

**Author:** ![nate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e95f7d/32.png) [@nate](https://www.ruby-forum.com/u/nate)\
**Post date:** [March 20, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-inside-of-an-association/163489/2 "2009-03-20T23:40:35Z")

</div>

On 20 Mar., 20:57, Nate [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= link\_to(“Delete”, { :action =\> “destroy”, :id =\> @bin }, :confirm  
> =\> “Are you sure?”, :method =\> :delete) %\>
> 
> This just throws back an error saying that there is not a Client with  
> that id.
> 
> I can’t figure out how to tell it to delete the bin not the client  
> with that id.
> 
> Any helpful suggestions? Cheers.

I guess it’s because, that when you’re rendering partials you’ll not  
be given an instance variable, but a local variable. You can try to  
see if this is enough to fix the problem:

\<%= link\_to(“Delete”, { :action =\> “destroy”, :id =\>  
bin.id }, :confirm  
=\> “Are you sure?”, :method =\> :delete) %\>

I simply removed the @ to reference a local variable instead. (And I  
call the #id method on the Bin, but that’s optional.)

–  
Cheers,  
David K.  
[http://twitter.com/rubyguy](http://twitter.com/rubyguy)

---

<div class="post-metadata">

**Author:** ![nate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e95f7d/32.png) [@nate](https://www.ruby-forum.com/u/nate)\
**Post date:** [March 21, 2009, 12:12am UTC](https://www.ruby-forum.com/t/how-to-delete-a-record-inside-of-an-association/163489/3 "2009-03-21T00:12:14Z")

</div>

On Mar 20, 10:39 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > records in Bin associated with Client.  
> > with that id.
> 
> > Any helpful suggestions? Cheers.
> 
> I guess it’s because, that when you’re rendering partials you’ll not  
> be given an instance variable, but a local variable. You can try to  
> see if this is enough to fix the problem:

No. The problem is the current controller is the client controller,  
so :action =\> destroy routes you to /clients/destroy, which  
unsurprisingly destroys clients not bins. If you’re feeling old school  
you would add :controller =\> ‘bins’ to your hash, but a more modern  
way to do this would be

link\_to ‘Delete’, bin\_path(@bin), :method =\> :delete

Fred
