2016-06-29 6 views
1

私はUserモデルとユーザーを持っているが、多くのフォロワーを持っており、以下:接続を介して:レール:信者のためのテーブル経由にhas_many、以下

has_many :connections 
has_many :followers, :through => :connections, :foreign_key => :follower_id, :source => :user, class_name: User 
has_many :following, :through => :connections, :foreign_key => :user_id, :source => :user, class_name: User 

私の接続テーブルがUSER_ID(ユーザーが守られている)とfollower_idを持っています(フォローしているユーザー)。

私は、新しい接続を作成することができます

Connection.create(user_id: 1, follower_id: 500) 

私は接続が作成されていることを確認し、まだ私は、それぞれのユーザーからこれをアクセスすることはできません。

u = User.find(1) 
u.followers => [] 
u.following => [] 

u = User.find(500) 
u.followers => [] 
u.following => [] 

はどこで間違ったんですか?

答えて

0

最初の関係では、ソースは:userですが、:followerとする必要があります。

その後編集:

上記の修正1つの問題が、完全な解決策は、2つの多くの接続がある必要があります。follwers用と、次のために1:

class User < ActiveRecord::Base 
    has_many :followers_connections, foreign_key: :user_id, class_name: "Connection" 
    has_many :following_connections, foreign_key: :follower_id, class_name: "Connection" 
    has_many :followers, through: :followers_connections, class_name: "User", source: :follower 
    has_many :following, through: :following_connections, class_name: "User", source: :user 
end 


class Connection < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :follower, class_name: "User" 
end 


# create some sample data 
5.times { User.create } 
Connection.create user_id: 1, follower_id: 2 
Connection.create user_id: 1, follower_id: 3 
Connection.create user_id: 4, follower_id: 1 
Connection.create user_id: 5, follower_id: 1 

# get followers 
User.find(1).followers.pluck(:id) => [2, 3] 
# SQL run: SELECT "users"."id" FROM "users" INNER JOIN "connections" ON "users"."id" = "connections"."follower_id" WHERE "connections"."user_id" = ? [["user_id", 1]] 

# get following 
User.find(1).following.pluck(:id) => [4, 5] 
# SQL run: SELECT "users"."id" FROM "users" INNER JOIN "connections" ON "users"."id" = "connections"."user_id" WHERE "connections"."follower_id" = ? [["follower_id", 1]] 
+0

恐ろしい - それはu.followersに取り組んで、まだuのための空白として現れています。以下。どんな考え? –

+0

ユーザー1はユーザー2の後に開始します。私はuser1.following ==> []を呼び出します。 user2.following ==> [user2] –

+0

接続には2つのhas_many関係が必要です。上記の例を数分で投稿する – bcd

0

アップデートの関連付けを以下のように、彼らがすべきあなたのために働く。

has_many :connections 
has_many :followers, through: :connections, class: 'User' 
has_many :following, through: :connections, foreign_key: :follower_id, source: :user 

説明:user_idconnectionsでテーブルを見込ん
1)あなたが接続を通じてユーザーにhas_manyのフォロワーを言う、
Railsの。私たちの場合は、user_idconnectionsにあります。
レールは接続中にfollower_idが必要です。私たちのケースでは、接続にfollower_idがあります。しかしここからfollower_idのレールはfollowerをモデルにしており、モデル名はfollowerではありません。したがって、followersの検索にはclass: 'User'を使用する必要があります。

2)あなたはユーザーにhas_manyが接続を通じて、次の言う、connections表にuser_idを期待
Railsの。この場合、接続にはuser_idがありますが、このuser_idを使用してfollowingを取得する必要はありません。followingを取得するにはfollower_idが必要です。foreign_key: :follower_idとする必要があります。したがって、Railsはfollower_idを外部キーとして使用します。
レールは、following_idconnectionsに設定します。私たちの場合、connectionsテーブルにfollowing_idがありません。したがってsource: :userと言及する必要があります。このレールはUserモデルです。これは有効です。

出典:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many

関連する問題