Как сделать рефлексивные отношения самостоятельного соединения в ActiveRecord?
Я пытаюсь реализовать модель дружбы в стиле социальных сетей, и мне не особо повезло, пытаясь выяснить, какие плагины доступны. Я думаю, что я лучше изучу Rails, если сделаю это сам. Вот что у меня есть:
class User < ActiveRecord::Base
has_many :invitee_friendships ,
:foreign_key => :friend_id,
:class_name => 'Friendship'
has_many :inviter_friends,
:through => :invitee_friendships
has_many :inviter_friendships ,
:foreign_key => :user_id,
:class_name => 'Friendship'
has_many :invited_friends,
:through => :inviter_friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
//I think something needs to come here, i dont know what
end
Вirb
когда я пытаюсь это:
friend1 = Friend.create(:name => 'Jack')
friend2 = Friend.create(:name => 'John')
bff = Friendship.create(:user_id =>1, :friend_id => 2)
f1.invited_friends
Я получаю ошибку:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
Could not find the source
association(s) :invited_friend or
:invited_friends in model Friendship.
Try 'has_many :invited_friends,
:through => :invited_friendships,
:source => <name>'. Is it one of
:user?
Expanation of friendship system:
A user can invite other users to become friends. Users who you invited to become friends are represented byinvited_friends
.
Users who invited you to become friends are represented by inviter_friends
.
Your total friend list is represented by invited_friends
+ inviter_friends
.
Schema
table Friendship
t.integer :user_id
t.integer :friend_id
t.boolean :invite_accepted
t.timestamps
table User
t.string :name
t.string :description