Skip to content

Commit

Permalink
Added dependent destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
tinogomes committed Apr 27, 2009
1 parent a30d272 commit 8739a75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class Friendship < ActiveRecord::Base
# associations
belongs_to :user
belongs_to :friend, :class_name => 'User', :foreign_key => 'friend_id'
belongs_to :message, :class_name => "FriendshipMessage", :foreign_key => "friendship_message_id"
belongs_to :message, :class_name => "FriendshipMessage", :foreign_key => "friendship_message_id", :dependent => :destroy

has_many :friendship_relations, :class_name => "FriendshipRelationType", :readonly => true
has_many :relations, :through => :friendship_relations
has_many :friendship_relations, :class_name => "FriendshipRelationType", :readonly => true, :dependent => :destroy
has_many :relations, :through => :friendship_relations, :class_name => "RelationType", :source => :relation

# callback
after_destroy do |f|
Expand Down
7 changes: 7 additions & 0 deletions lib/relation_type.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class RelationType < ActiveRecord::Base
validates_presence_of :name

after_destroy :destroy_all_friendsip_relations

private
def destroy_all_friendsip_relations
FriendshipRelationType.delete_all :relation_id => self.id
end
end

0 comments on commit 8739a75

Please sign in to comment.