2012-01-05 4 views
0

다음과 같은 트위터와 유사한 모델을 설정했습니다. 사용자는 모두 서로 구독 할 수 있습니다. 관계를 만들려고 할 때 내 사용자 컨트롤러에 오류가 발생합니다.다자간 연결이 실패하는 이유는 무엇입니까?

user.rb :

has_many :subscriptions 
has_many :providers, :through => :subscriptions 

has_many :followings, :class_name => "Subscription" 
has_many :followers, :through => :followings 

subscription.rb

belongs_to :provider, :class_name => 'User', :foreign_key => "provider_id" 
belongs_to :follower, :class_name => 'User', :foreign_key => "follower_id" 

users_controller.rb

69 def follow 
70 logger.debug params.to_yaml 
71 @user = User.find(params["user_id"]) 
72 logger.debug @user.to_yaml 
73 if current_user.providers << @user 
74  flash[:notice] = "Subscribed" 
75 else 
76  flash[:error] = "Unable to subscribe." 
77 end 
78 end 

내가 전화 할 때 이것은 다음과 오류입니다 :

ActiveRecord::UnknownAttributeError (unknown attribute: user_id): 
    app/controllers/users_controller.rb:73:in `follow' 
,536,

rake db : migrate를 실행했음을 확인했습니다. subscription 테이블에는 provider_id와 follower_id 두 개의 필드가 있습니다. 누구든지 오류를 도와 줄 수 있고 'user_id'속성을 찾는 이유를 설명 할 수 있습니까?

업데이트 :

show.html.erb :

<%= button_to "Subscribe", user_follow_path(@user), :remote => true %> 

routes.rb :

resources :users do 
    resources :locations 
    resources :saved_events 
    resources :saved_locations 
    post "follow" 
end 

레이크 노선 | 그렙 추적 : 당신이 제공 컬렉션 <<를 호출 할 때 사용자가이 추종자해야하는데 모르기 때문에

user_follow POST  /users/:user_id/follow(.:format)     {:action=>"follow", :controller=>"users"} 
+0

당신이 코드에서 users_controller.b에서 라인 73에 주석을 달 수 있습니까? – klochner

답변

1

, 나는 그들이 정상적으로 해당 모음 기능이 작동하도록 데이터 모델을 수정이 솔루션을 함께했다.

공급자 ID를 액세스 가능하게 만들고 구독 모델에서 외래 키를 제거하십시오.

subscription.rb :

attr_accessible :provider_id 
belongs_to :provider, :class_name => 'User' 
belongs_to :follower, :class_name => 'User' 

는 사용자 모델에서 구독 및 reverse_subscriptions에 대한 외래 키를 추가합니다.

user.rb는 :

has_many :subscriptions, :foreign_key => "subscriber_id", :dependent => :destroy 
has_many :subscribed_to, :through => :subscriptions, :source => :provider 

has_many :reverse_subscriptions, :class_name => "Subscription", :foreign_key => "provider_id", :dependent => :destroy 
has_many :followers, :through => :reverse_subscriptions 

또한 사용자 모델에 도우미 방법을 추가했습니다.

사용자rb :

def following?(provider) 
    subscriptions.find_by_provider_id(provider) 
end 

def follow!(provider) 
    subscriptions.create!(:provider_id => provider.id) 
end 

def unfollow!(provider) 
    subscriptions.find_by_provider_id(provider).destroy 
end 

그런 다음 컨트롤러에서 다음을 호출 할 수 있습니다! 또는 unfollow!

user_controller.rb :

... 
current_user.unfollow!(@user) 
... 
current_user.follow!(@user) 
... 
0

당신에게 오류를주고있는 이유입니다. 그래서 기본적으로 말합니다, "저는 사용자입니다.이 제공자 모음에 나를 추가하십시오!" 대신 "이 사람은 나를 따라 가고, 지금은 공급자 해요 그는 추종자이다"

가장 간단한 대답은 단지

user.rb 할 수 있습니다 :

def follow(other_user) 
    Subscription.create(:provider => other_user, :follower => self) 
end 

users_controller을 .rb 가이드로 Michael Hartl's tutorial를 사용

def follow 
    @user = User.find(params["user_id"]) 
    if current_user.follow(@user) 
    flash[:notice] = "Subscribed" 
    else 
    flash[:error] = "Unable to subscribe." 
    end 
end 
+0

나는 before_filter : before_filter : authenticate_user !, : only => [: update, : follow]' – Stanish

+0

을 가지고 있습니다. – Azolo

+0

귀하의 편집 내용이 구독 또는 사용자 모델을 지칭합니까? – Stanish

관련 문제