This is probably pretty simple but I’m not quite sure how to implement it. I have Events and Users and they are joined by HABTM. When a user is deleted, I would like all their associations with events to be deleted as well. Also, when an Event is deleted, I would like all user associations with that event to be delete. (not the users themselves but just the association) Thanks! Here’s my assocations:
Event:
has_and_belongs_to_many :users, :uniq => true
User:
has_and_belongs_to_many :events, :uniq => true
Try :dependents => :destroy. Good luck.
May be another requirement, check out more in the docs.
You can also try, my_object.my_dependents.destroy_all. But considering in a HABTM relationship, this could cause everything to get deleted if they are all tied together, just seems a bit sketchy, especially if it goes both ways.
A note from the docs and linkage:
Choosing which way to build a many-to-many relationship is not always simple. If you need to work with the relationship model as its own entity, use has_many :through. Use has_and_belongs_to_many when working with legacy schemas or when you never work directly with the relationship itself.
Link to read more: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html