udi*_*dit 4 activerecord ruby-on-rails self-join social-networking
我正在尝试实现一个社交网络风格的友谊模型,我没有太多的运气试图找出那里可用的插件.如果我自己做的话,我想我会更好地学习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
Run Code Online (Sandbox Code Playgroud)
在irb当我尝试这样的:
friend1 = Friend.create(:name => 'Jack')
friend2 = Friend.create(:name => 'John')
bff = Friendship.create(:user_id =>1, :friend_id => 2)
f1.invited_friends
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
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?
Run Code Online (Sandbox Code Playgroud)
友谊系统的扩展:
invited_friends.inviter_friends.invited_friends+ 表示inviter_friends.架构
table Friendship
t.integer :user_id
t.integer :friend_id
t.boolean :invite_accepted
t.timestamps
table User
t.string :name
t.string :description
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9369 次 |
| 最近记录: |