Search code examples
ruby-on-railsruby-on-rails-3dependent-destroy

Rails, How to setup a Dependent Destroy for a nested model set?


Possible Duplicate:
Rails - Help understanding how to use :dependent => :destroy

I have the following models:

User
Permission (user_id, group_id)
Group
Conversation (group_id)
ConversationParticipation (conversation_id)

What I want to do in my Permissions model is, when a permission is destory, delete all the related ConversationParticipations based on the group_id and user_id.

I tried this:

class Permission < ActiveRecord::Base
has_many :conversation_participations, :through => :group, :source => :conversations, :dependent => :destroy

But that doesn't seem to be cutting it just yet. Suggestions?

Thanks


Solution

  • Part of the Rails Documentation for has_many

    :dependent

    If set to :destroy all the associated objects are destroyed alongside this object by calling their destroy method. If set to :delete_all all associated objects are deleted without calling their destroy method. If set to :nullify all associated objects’ foreign keys are set to NULL without calling their save callbacks. If set to :restrict this object cannot be deleted if it has any associated object.

    Warning: This option is ignored when used with :through option.

    You could always try callbacks.