Relationships and Deleting

I am playing around with relationships at the moment and am hitting a

I have a group.

class Group < ActiveRecord::Base
has_many :contacts

and a contact.

class Contact < ActiveRecord::Base
belongs_to :group

If I destroy group it doesn’t destroy contacts in the group.

Am I missing something obvious?


To delete contacts when the group is deleted:

has_many :contacts, :dependent => :destroy

or if you don’t use callbacks (before_destroy, after_destroy) in
this is faster:

has_many :contacts, :dependent => :delete_all

Thanks lx Quic!

Very informative.

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