May be you could use model callbacks, e.g.:
class Deal < ActiveRecord::Base
belongs_to :account
belongs_to :contact
before_update :nullify_contact_association, :if => lambda{|i| i.account_id_changed?}
private
# Nullify contact_id of the deal if it's account association was changed
def nullify_contact_association
self.contact_id = nil
end
end
class Account < ActiveRecord::Base
has_many :contacts
has_many :deals
before_destroy :nullify_dependencies
private
#Deleting an account associated with a deal should
#make sure that contact_id of that deal table is nullified
def nullify_dependencies
self.deals.update_all(:contact_id => nil) if deal.present?
end
end
class Contact < ActiveRecord::Base
belongs_to :account
has_many :deals
before_destroy :nullify_dependencies
private
#Deleting a contact from an account should make sure
#that corresponding contact_id of the deal table is set to nil
def nullify_dependencies
self.deals.update_all(:contact_id => nil) if account.present?
end
end