0

To make a self referential relationship in active record, I have followed the self-referential model shown in rails-cast #163 http://railscasts.com/episodes/163-self-referential-association, but I don't like this model because I then have to write friendship and inverse_friendships, couldn't I just do something like:

in my person.rb

class Person 
  has_many :friendships
  has_many :friendships, :foreign_key => "friend_id"
  has_many :friends, :through => :friendships
  has_many :friends, :through => :friendships, :source => :person
end

and in my friendship.rb

class Friendship
  belongs_to :person
  belongs_to :friend, :class_name => "Person"
end
tereško
  • 58,060
  • 25
  • 98
  • 150
OneChillDude
  • 7,856
  • 10
  • 40
  • 79

1 Answers1

2

No you can't have similarly named associations, these names need to be unique within the model. Otherwise, how will ActiveRecord know which friendships you mean when you have @user.friendships in your code?

PinnyM
  • 35,165
  • 3
  • 73
  • 81