我该怎么做的ActiveRecord的反身自联接关系? [英] How do I do reflexive self-join relationships in ActiveRecord?

查看:77
本文介绍了我该怎么做的ActiveRecord的反身自联接关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个社交式的友谊模式,我没有太多的运气,试图找出插件可用在那里。我想我会学习Rails的更好,如果我做我自己。所以,这里是我的:

 类用户的LT;的ActiveRecord :: Base的
  的has_many:invitee_friendships,
           :foreign_key => :friend_id,
           :将class_name => '友谊'

  的has_many:inviter_friends,
            :通过=> :invitee_friendships

  的has_many:inviter_friendships,
           :foreign_key => :用户帐号,
           :将class_name => '友谊'

  的has_many:invited_friends,
            :通过=> :inviter_friendships

结束

一流的友谊<的ActiveRecord :: Base的
  belongs_to的:用户
  //我认为有些事情需要到这里来,我不知道是什么
结束
 

IRB 当我尝试这样的:

  friend1 = Friend.create(:名称=>'杰克')
friend2 = Friend.create(:名称=>约翰)
BFF = Friendship.create(:USER_ID =大于1,:friend_id =→2)
f1.invited_friends
 

我得到一个错误:

 的ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:
无法找到源
联想(S):invited_friend或
:在模型中友谊invited_friends。
试试'的has_many:invited_friends,
:通过=> :invited_friendships,
:源=> <名称>。它是一个
:用户?
 

友谊体系Expanation:

  • 的用户可以邀请其他用户成为朋友。
  • 通过 invited_friends psented
  • 您邀请谁成为朋友用户重新$ P $。
  • 谁邀请你的用户成为朋友是通过 inviter_friends psented重新$ P $。
  • 通过 invited_friends + inviter_friends psented
  • 您的总好友列表重新$ P $。

模式

 表友谊
      t.integer:USER_ID
      t.integer:friend_id
      t.boolean:invite_accepted
      t.timestamps

表用户
    t.string:名称
    t.string:描述
 

解决方案

我很惊讶,没有人提到最近瑞安贝茨的的在话题:)截屏

希望这有助于!

这是瑞安摘录......需要在用户模型中的自我指涉协会定义的朋友/追随者

I'm trying to implement a social networking style friendship model and I didnt have much much luck trying to figure out the plugins available out there. I think I'll learn Rails better if I do it myself. So here's what I have :

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 when I try this:

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

I get an error:

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 by invited_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

解决方案

I'm surprised no one has pointed to the recent Ryan Bates's screencast on the topic :)

Hope this helps!.

Excerpt from Ryan '... requires a self-referential association on the User model to define friends/followers'

这篇关于我该怎么做的ActiveRecord的反身自联接关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆