2011-08-15 19 views
0

我正在處理這樣的事情。我正在爲Rails中的應用程序開發朋友系統。問題是,它刪除/更新每個user_id列值而不是僅爲用戶建立連接的兩個值。爲什麼它會刪除我不想要的rown?

例如,如果用戶1向朋友2發送邀請給朋友,則有兩行 - [1,2,r]和[2,1,p]。 R表示請求,P表示待處理。在這種情況下,當用戶3和4向用戶2發送邀請時,則2有3個邀請。現在,如果該用戶刪除或更新了其中一個邀請,那麼每個邀請都會被刪除/更新(在這種情況下會被接受)。我想通過刪除/更新正確的行來使其正常工作。

下面是代碼:

用戶模型:

class User < ActiveRecord::Base 
    has_many :friends, :through => :user_friendships, :conditions => "status = 'a'" 
    has_many :requested_friends, :through => :user_friendships, :source => :friend, :conditions => "status = 'r'", :order => "user_friendships.created_at" 
    has_many :pending_friends, :through => :user_friendships, :source => :friend, :conditions => "status = 'p'", :order => "user_friendships.created_at" 
    has_many :user_friendships, :dependent => :destroy 
... 

UserFriendship模型:

class UserFriendship < ActiveRecord::Base 
    set_primary_key "user_id" 

    attr_accessible :user_id, :friend_id, :status 

    belongs_to :user 
    belongs_to :friend, :class_name => "User", :foreign_key => "friend_id" 
end 

好友控制器:

class FriendsController < ApplicationController 
    before_filter :load_user, :except => [:index, :show] 

    def index 
    @user = User.find_by_name(params[:user_id]) 
    if @user.nil? 
     flash[:Error] = t "generic.messages.error.user_not_exist" 
     redirect_to root_path 
    else 
     @title = @user.name 
    end 
    end 

    def show 
    redirect_to user_path(params[:id]) 
    end 

    def new 
    @friendship1 = UserFriendship.new 
    @friendship2 = UserFriendship.new 
    end 

    def create 
    @friend = User.find_by_name(params[:friend_id]) 
    params[:friendship1] = {:user_id => @user.id, :friend_id => @friend.id, :status => 'r'} 
    params[:friendship2] = {:user_id => @friend.id, :friend_id => @user.id, :status => 'p'} 
    @friendship1 = UserFriendship.create(params[:friendship1]) 
    @friendship2 = UserFriendship.create(params[:friendship2]) 
    if @friendship1.save && @friendship2.save 
     flash[:Info] = t "generic.friends.request_send" 
     redirect_to :back 
    else 
     flash[:Error] = t "generic.friends.request_error" 
     redirect_to :back 
    end 
    end 

    def update 
    @friend = User.find_by_name(params[:id]) 
    params[:friendship1] = {:user_id => @user.id, :friend_id => @friend.id, :status => 'a'} 
    params[:friendship2] = {:user_id => @friend.id, :friend_id => @user.id, :status => 'a'} 
    @friendship1 = UserFriendship.find_by_user_id_and_friend_id(@user.id, @friend.id) 
    @friendship2 = UserFriendship.find_by_user_id_and_friend_id(@friend.id, @user.id) 
    if @friendship1.update_attributes(params[:friendship1]) && @friendship2.update_attributes(params[:friendship2]) 
     flash[:Success] = t "generic.friends.added_to_friends" 
     redirect_to :back 
    else 
     flash[:Error] = t "generic.friends.added_error" 
     redirect_to :back 
    end 
    end 

    def destroy 
    @friend = User.find_by_name(params[:id]) 
    @friendship1 = UserFriendship.find_by_user_id_and_friend_id(@user.id, @friend.id).destroy 
    @friendship2 = UserFriendship.find_by_user_id_and_friend_id(@friend.id, @user.id).destroy 
    flash[:Info] = t "generic.friends.friend_removed" 
    redirect_to :back 
    end 

    private 

    def load_user 
     authenticate 
     correct_user 
    end 

    def authenticate 
     deny_access unless signed_in? 
    end 

    def correct_user 
     @user = current_user 
     redirect_to(root_path) unless current_user?(@user) 
    end 

end 

例DB: enter image description here

回答

0

答案是簡單地將ID添加到數據庫。我不確定,爲什麼這是...

相關問題