为什么它删除了我不想要的rown?

时间:2011-08-15 13:38:59

标签: ruby-on-rails-3

我正在处理这样的事情。我正在为Rails中的应用程序开发朋友系统。问题是它删除/更新了每个user_id列值而不是只有两个为用户建立连接的值。

例如,如果用户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

1 个答案:

答案 0 :(得分:0)

答案就是简单地将id添加到db。我不确定,为什么会这样......