# AR - Delete m:n Associations when deleting Record

**URL:** <https://www.ruby-forum.com/t/ar-delete-m-n-associations-when-deleting-record/134927>\
**Category:** Rails\
**Created:** [April 16, 2008, 8:44am UTC](https://www.ruby-forum.com/t/ar-delete-m-n-associations-when-deleting-record/134927 "2008-04-16T08:44:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [April 16, 2008, 8:44am UTC](https://www.ruby-forum.com/t/ar-delete-m-n-associations-when-deleting-record/134927/1 "2008-04-16T08:44:16Z")

</div>

Hey!

I have three tables in my DB

users \<–\> users\_courses \<–\> courses

users and courses are m:n related:

User:  
has\_many :courses, :through =\> :user\_courses

Course  
has\_many :users, :through =\> :user\_courses

I want to delete all records for a user in user\_courses when deleting  
the user. How do i achieve that?

thx

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [April 16, 2008, 9:52am UTC](https://www.ruby-forum.com/t/ar-delete-m-n-associations-when-deleting-record/134927/2 "2008-04-16T09:52:32Z")

</div>

Take a look at :dependent option in has\_many method.
