开发者

Why it deletes rown I don't want?

I'm dealing with such thing. I'm working on friends system for my application in Rails. And what the problem is that it deletes/updates each of the user_id column values instead of only two which makes connection for the users.

For example, if user 1 send invitation to friends to user 2. There comes two rows - [1, 2, r] and [2, 1, p]. R for requested, P for pending. At this situtation, when user 3 and 4 send invitation for user 2, then 2 has 3 invitations. Now if that user deletes or updates one of the invitation, then each of those invitations are deleted/updated (accepted in this situation). I want to make it working properly, by deleting/updating only proper rows.

Here is the code:

User model:

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 model:

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

Friends controller:

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(para开发者_如何学JAVAms[: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

Example DB:

Why it deletes rown I don't want?


The answer to this was to simply add id to the db. I'm not sure, why it's that...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜