Hi, I'm building a social networking website. I have a table of Users. Each user can have a number of other users as friends, so I have a second table Friends:
user_id
friend_id
Based on this answer I'm trying to create the relationships. I have,
class User < ActiveRecord::Base
has_many :friends, :dependent => :destroy
has_many :users, :through => :friends
has_many :source_friend, :class_name => "Friend", :foreign_key => "friend_id", :dependent => :destroy
has_many :source_users, :class_name => "User", :through => :friend_id
...
and
class Friend < ActiveRecord::Base
belongs_to :user
belongs_to :source_friend, :class_name => "User", :foreign_key => "friend_id"
end
When I try and use the tables with this in my view:
<% @user.users.each do |friend| %>
<% if friend.status == User::ACTIVE %>
<p>
<%= link_to h(friend.name), :controller => "user", :id => h(friend.name)%>
</p>
<% end %>
<% end %>
the names returned are always those of the source user, rather than the target friend.