My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members

This plugin is based on Chad Fowler's Rails Recipe #18 : Self-referential Many-to-Many Relationships in Rails Recipe book.

This plugin simplifies self referential many to many relationship. It manages the bidirectional link creation and deletion automatically.

For example:

class Person < ActiveRecord::Base

has_and_belongs_to_many :friends,
:class_name => "Person", :join_table => "friends_people", :association_foreign_key => "friend_id", :foreign_key => "person_id", :after_add => :be_friendly_to_friend, :after_remove => :no_more_mr_nice_guy
def be_friendly_to_friend(friend)
friend.friends << self unless friend.friends.include?(self)
end
def no_more_mr_nice_guy(friend)
friend.friends.delete(self) rescue nil
end
end

becomes:

class Person < ActiveRecord::Base

has_self_referential_many_to_many :friends
end

Powered by Google Project Hosting