Remove association

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

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

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.
[email protected]
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/.


Ryan B.
http://www.frozenplague.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs