
Untitled
By: a guest on
Jul 6th, 2012 | syntax:
None | size: 0.46 KB | hits: 11 | expires: Never
What's the best way to destroy both sides of a self-referential association?
class User < ActiveRecord::Base
has_many :relationships, :dependent => :destroy
has_many :peers, :through => :relationships
end
class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :peer, :class_name => "User"
end
after_destroy do |record|
other = Relationship.find_by_user_id_and_peer_id(record.peer_id, record.user_id)
other.destroy if other
end