Report abuse

User model (your table D)


			
class User < ActiveRecord::Base
  has_many    :friendships, :foreign_key => :fan_id, :class_name => "Friendship", :dependent => :destroy
  has_many    :fanships, :foreign_key => :friend_id, :class_name => "Friendship", :dependent => :destroy
  has_many    :friends, :through => :friendships
  has_many    :fans, :through => :fanships
end

Friendship class (your table R)


			
class Friendship < ActiveRecord::Base
  belongs_to  :fan, :foreign_key => :fan_id, :class_name => 'User'
  belongs_to  :friend, :foreign_key => :friend_id, :class_name => 'User'
  has_and_belongs_to_many :relationships
end

Relationship class (your table A)


			
class Relationship
  has_and_belongs_to_many :friendships
end

the migration for friendships


			
create_table :friendships do |t|
  t.column :fan_id,      :integer
  t.column :friend_id,    :integer
end

the migration for relationship


			
create_table :relationships do |t|
  t.column :status,      :string
end

the migration for friendships_relationships


			
create_table :friendships_relationships, :id => false do |t|
  t.column :friendship_id,      :integer
  t.column :relationship_id,    :integer
end

usage?


			
bob = User.create(:name => 'bob')
sam = User.create(:name => 'sam')
r = Relationship.create(:status => 'good')

f = bob.friendships.create(:friend => sam)
f.relationships << r

# so f is a Friendship which has connection to two users and a relationship
#