# Remove association

**URL:** <https://www.ruby-forum.com/t/remove-association/196550>\
**Category:** Rails\
**Created:** [December 20, 2007, 11:17am UTC](https://www.ruby-forum.com/t/remove-association/196550 "2007-12-20T11:17:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 20, 2007, 11:17am UTC](https://www.ruby-forum.com/t/remove-association/196550/1 "2007-12-20T11:17:21Z")

</div>

Hi all,

I have a user has many numbers relationship.

When I query User.find(:all, :include =\> :numbers) I get all the users  
with their numbers. I’m looking for a method that removes the number  
from a user eg users[0].numbers[1].delete . I basically want ot remove  
the association but I’m unable to find such a method.

Regards,  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 20, 2007, 11:24am UTC](https://www.ruby-forum.com/t/remove-association/196550/2 "2007-12-20T11:24:24Z")

</div>

collection.delete(object, â€¦) - removes one or more objects from the  
collection by setting their foreign keys to NULL. This will also destroy  
the objects if theyâ€˜re declared as belongs\_to and dependent on this  
model.

so in your case:

users[0].numbers.delete(users[0].numbers[0])

users[0].numbers.delete\_all # would kill everything

use  
has\_mane :numbers, :dependent =\> destroy

if you want it to destroy the records in the db, too

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [December 20, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/remove-association/196550/3 "2007-12-20T12:23:10Z")

</div>

So you just want to unassociate that number from a user?

number = Number.find(params[:id])  
number.update\_attribute(“user\_id”,nil)

On Dec 20, 2007 8:54 PM, Thorsten M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> users[0].numbers.delete\_all # would kill everything
> 
> use  
> has\_mane :numbers, :dependent =\> destroy
> 
> ## if you want it to destroy the records in the db, too
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!
