Domanda

Sto cercando di implementare un modello di amicizia in stile social network e non ho avuto molta fortuna nel cercare di capire i plugin disponibili là fuori. Penso che imparerò meglio Rails se lo faccio da solo. Quindi, ecco quello che ho:

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

In irb quando provo questo:

friend1  = Friend.create(:name => 'Jack')
friend2  = Friend.create(:name => 'John')
bff = Friendship.create(:user_id =>1, :friend_id => 2)
f1.invited_friends

Ricevo un errore:

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?

Espansione del sistema di amicizia:

  • Un utente può invitare altri utenti a diventare amici.
  • Gli utenti che hai invitato a diventare amici sono rappresentati da invitati_friends .
  • Gli utenti che ti hanno invitato a diventare amici sono rappresentati da inviter_friends .
  • Il tuo elenco di amici totale è rappresentato da invitati_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
È stato utile?

Soluzione

Sono sorpreso che nessuno abbia segnalato il recente screencast sull'argomento :)

Spero che questo aiuti !.

Estratto da Ryan '... richiede un'associazione autoreferenziale sul modello Utente per definire amici / follower'

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top