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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 theyre 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Mueller <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---