2011-12-22 5 views
0

問題:@ user.friendsは機能しません。何らかの理由でフレンドシップモデルアソシエーションをレールに作成する

class User < ActiveRecord::Base 
    has_many :friendships 
    has_many :friends, 
    :through => :friendships, 
    :conditions => "status = 'accepted'", 
    :order => :fname 
    has_many :requested_friends, 
    :through => :friendships, 
    :source => :friend, 
    :conditions => "status = 'requested'" 
    has_many :pending_friends, 
    :through => :friendships, 
    :source => :friend, 
    :conditions => "status = 'pending'" 

class Friendship < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :friend, :class_name => "User", :foreign_key => "friend_id" 

:私は次のモデルを持っていることは、2つのレコードを返すだし、それは4 ...

でなければなりません。 <%[email protected]%>は、すべてのユーザーの友だちを返すわけではありません。

例データ:user.friends.length @

> @user.friendships.all.length 
=> 4 
> @user.friendships 
=> [#<Friendship id: 20, user_id: 11, friend_id: 20, status: "accepted", created_at: "2011-12-22 12:59:22", updated_at: "2011-12-22 17:02:54">, #<Friendship id: 8, user_id: 11, friend_id: 12, status: "accepted", created_at: "2011-12-22 06:29:02", updated_at: "2011-12-22 07:41:24">, #<Friendship id: 3, user_id: 11, friend_id: 1, status: "approved", created_at: "2011-12-22 05:48:29", updated_at: "2011-12-22 06:22:09">, #<Friendship id: 1, user_id: 11, friend_id: 641, status: "approved", created_at: "2011-12-22 04:47:19", updated_at: "2011-12-22 04:47:19">] 
> @user.friends.length 
=> 2 

データとして4ていなければならないが、上記の "受け入れられた" などのすべての状態を示しています。私が上にリストアップされたモデル協会でうんざりしたどんなアイデア?

ありがとうございました!

答えて

3

ステータスは友情の2のための「を承認した」と2用「を受け入れた」です。あなたの条件に応じてが受け入れられたのはの友情だけです。

+0

ありがとうございます! – AnApprentice

関連する問題