# Controlling join table actions based on join table content

**URL:** <https://www.ruby-forum.com/t/controlling-join-table-actions-based-on-join-table-content/175372>\
**Category:** Rails\
**Created:** [September 11, 2009, 10:40pm UTC](https://www.ruby-forum.com/t/controlling-join-table-actions-based-on-join-table-content/175372 "2009-09-11T22:40:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [September 11, 2009, 10:40pm UTC](https://www.ruby-forum.com/t/controlling-join-table-actions-based-on-join-table-content/175372/1 "2009-09-11T22:40:11Z")

</div>

I have an active record association that when I delete the parent  
object the ::dependent =\> :nullify option works correctly and sets  
the FKs to nil in the join table. Is there a way to make this happen  
when the record is updated too? When I remove an association from  
the parent, the association is deleted instead of setting the FK to  
nil.

To complicate things there are times when I would like the behavior to  
be :dependent =\> :destroy\_all (depending on what is in the join  
table).

I am using has\_many :through and depending on what is in my join table  
I may want to destroy instead of nullify.

Is this a job for an observer on the join table or maybe overriding  
the destroy method :-\

## Fabricated Example (i need a better example)

Lets say there is a company that manages shooting locations for  
movies. The company offers 2 types of contracts (transferable and  
fixed).

class Movie \< ActiveRecord::Base  
has\_many :contracts, :dependent =\> :nullify  
has\_many :locations, :through =\> :contracts  
end  
class Contract \< ActiveRecord::Base  
belongs\_to :location # location\_id is unique.  
belongs\_to :movie

# There is a contract type. fixed or transferable

# transferrable contracts can have a nil movie. A fixed one can not

end  
class Location \< ActiveRecord::Base  
has\_many :contracts  
has\_many :movies, :through =\> :contracts  
end

A movie may purchase several locations (contracts). If the movie is  
canceled, the locations are freed up (if using :dependent  
=\> :destroy\_all), If any locations are changed the unreferenced  
locations are deleted. This all works fine.

But what if I have a contract that allows me to transfer a location I  
purchased to another movie. If my contract\_type is transferable and I  
delete the movie the contract should remain but not reference any  
movie until I reassign it (dependent =\> :nullify). The contract  
should also remain if I remove just 1 location from my movie and I  
want to transfer it to another movie at a later point.

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [September 12, 2009, 1:04am UTC](https://www.ruby-forum.com/t/controlling-join-table-actions-based-on-join-table-content/175372/2 "2009-09-12T01:04:46Z")

</div>

overriding the Movie destroy method works fine for killing ‘fixed’  
contracts before ActiveRecord makes them null.

At 1st I was thinking of catching the nullify via a callback on the  
Contract class (wasn’t sure what I was going to do here but it appears  
the nullification doesn’t invoke the callbacks).

Next up. prevent location update on a movie object from deleting  
transferable contracts. I guess I’ll need to make locations  
attr\_protected and manually make updates to the contracts.
