2011-12-22 4 views
0

문제 : @ user.friends가 작동하지 않습니다. 이 두 기록을 반환 그리고 그것은 .. 4해야 레일에서 Friendships Model Association 만들기

나는 다음과 같은 모델이 : 어떤 이유

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" 

합니다. <%[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입니다. 그렇기 때문에 귀하의 조건에 따라 명의 우정을 수락하는 것입니다.

+0

대단히 감사합니다! – AnApprentice