2012-10-08 4 views
0

현재 첫 번째 프로젝트를 진행 중이며 내 앱에 "friending"을 추가하려고합니다. 그러나 내 데이터베이스에 존재하지 않는 속성이 작동하는 것처럼 보이기 때문에 우정 모델의 개념을 파악하는 데 약간의 어려움이 있습니다.레일 : 우정 조인 테이블 설명

아래 나열된 모델과 데이터베이스 열이 있습니다. 이 테이블은 조인 테이블이지만 사용자 모델에서는 friendships 테이블에없는 경우 friends, requested_friendspending friends이 표시됩니다. 가상 조인 테이블이기 때문에 내가 마이그레이션 할 필요없이 필요한 모든 열을 추가 할 수 있습니까?

또한 내 users 테이블에 해당 열이 없더라도 자습서는 결국 @user.pending_friends.each이됩니다. 나는이 부분에서 꽤 혼란 스럽다.

현재 나는이 간단한 튜토리얼을 통해 갈거야 :

사용자 모델

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

우정 모델

class Friendship < ActiveRecord::Base 
    attr_accessible :friend_id 

    belongs_to :user 
    belongs_to :friend, :class_name => "User" 
end 
: 여기

http://railsforum.com/viewtopic.php?id=16760 내 파일입니다

당신은 아마 당신이 당신의 스키마 status 열이 없기 때문에 마이그레이션 잊어

create_table "friendships", :force => true do |t| 
    t.integer "user_id" 
    t.integer "friend_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
end 

create_table "users", :force => true do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    t.string "password_digest" 
    t.string "remember_token" 
end 
+0

이것에 대한 결과는 무엇입니까? –

답변

0

스키마. 추천하는 협회는 friend_id 키에 바인딩되어 있습니다. 은 우정 테이블에입니다. 마지막 2 개의 연관은 belongs_to :friend에 정의 된 외래 키를 사용하고 있습니다. 제발 봐 here. 이 연관성은 실제 특성없이 작동 할 수 있다고 생각합니다. 이것이 의미하는 바라면 condition: 기능을 여기에서 얻지 않고 마지막 3 개의 연관성이 모두 같은 값을 반환합니다.