Jak wykonać refleksyjne relacje samodzielnego łączenia w ActiveRecord?
Próbuję zaimplementować model przyjaźni w stylu społecznościowym i nie miałem wiele szczęścia próbując znaleźć dostępne tam wtyczki. Myślę, że lepiej nauczę się Railsów, jeśli sam to zrobię. Oto co mam:
<code>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 </code>
Wirb
kiedy to wypróbuję:
<code>friend1 = Friend.create(:name => 'Jack') friend2 = Friend.create(:name => 'John') bff = Friendship.create(:user_id =>1, :friend_id => 2) f1.invited_friends </code>
Dostaję błąd:
<code>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? </code>
Rozszerzenie systemu przyjaźni:
Użytkownik może zaprosić innych użytkowników do zostania przyjaciółmi.Użytkownicy, których zaprosiłeś do zostania przyjaciółmi, są reprezentowani przezinvited_friends
.Użytkownicy, którzy zaprosili Cię do zostania przyjaciółmi, są reprezentowani przezinviter_friends
.Twoja całkowita lista przyjaciół jest reprezentowana przezinvited_friends
+ inviter_friends
.Schemat
<code>table Friendship t.integer :user_id t.integer :friend_id t.boolean :invite_accepted t.timestamps table User t.string :name t.string :description </code>