Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 0.46 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What's the best way to destroy both sides of a self-referential association?
  2. class User < ActiveRecord::Base
  3.   has_many :relationships, :dependent => :destroy
  4.   has_many :peers, :through => :relationships
  5. end
  6.        
  7. class Relationship < ActiveRecord::Base
  8.   belongs_to :user
  9.   belongs_to :peer, :class_name => "User"
  10. end
  11.        
  12. after_destroy do |record|
  13.   other = Relationship.find_by_user_id_and_peer_id(record.peer_id, record.user_id)
  14.   other.destroy if other
  15. end