在待处理的好友请求(友情关系模型)中查看未隐藏“添加好友”链接

时间:2012-03-29 22:36:33

标签: ruby-on-rails ruby-on-rails-3

如果是,我试图隐藏我的“添加朋友”链接    - current_user和@user是朋友    - 有待处理的朋友请求
   - current_user和@user具有相同的id(同一用户)

如果current_user和@user是朋友,则不会显示“添加朋友”链接。问题是当有待处理的朋友请求时。我提交好友请求后仍然会看到“添加好友”链接。

我认为这些条件有问题吗?

当我在控制台中运行这两个条件时: current_user.pending_friends.include?(@user)@user.pending_friends.include?(current_user),即使我的数据库显示待处理请求,我也会收到错误。

以下是我的观点:

用户/ show.html.erb

<% if signed_in? %>
  <% if @user == current_user || current_user.friends.include?(@user) 
        || current_user.pending_friends.include?(@user) 
        || @user.pending_friends.include?(current_user)  %>


   <% else %>      

    <%= link_to  friendships_path(:user_id => current_user.id, 
            :friend_id => @user.id), :method => :post, :action => 'create' do %>

    Add Friend

    <% end %>
  <% end %>
<% end %>

用户模型

has_many :friendships

has_many :inverse_friendships, 
 :class_name => "Friendship", 
 :foreign_key => "friend_id"

has_many :direct_friends, 
 :through => :friendships, 
 :conditions => "status = 'accepted'", 
 :source => :friend

has_many :inverse_friends, 
 :through => :inverse_friendships, 
 :conditions => "status = 'accepted'", 
 :source => :user

has_many :pending_friends, 
 :through => :friendships, 
 :conditions => "status = 'pending'", 
 :foreign_key => "user_id", 
 :source => :user   #this line is incorrect. see correction below.

has_many :requested_friends, 
 :through => :friendships,
 :source => :friend, 
 :conditions => "status = 'requested'"

def friends
  direct_friends | inverse_friends
end

以下是来自我的友谊控制器的创建动作,所以你们可以看到如何在数据库中创建关系:

def create
  @user = User.find(current_user)
  @friend = User.find(params[:friend_id])
  params[:friendship1] = {:user_id => @user.id, :friend_id => @friend.id, :status => 'pending'}
  params[:friendship2] = {:user_id => @friend.id, :friend_id =>@user.id, :status => 'requested'}
  @friendship1 = Friendship.create(params[:friendship1])
  @friendship2 = Friendship.create(params[:friendship2])

  if @friendship1.save && @friendship2.save
    flash[:success] = "Friend request submitted."
    redirect_to @friend
  else 
    flash[:error] = "Friend request failed."
    redirect_to @friend
  end  
end

感谢您的期待!

编辑:发现我的错误并在下面发布了答案。

来源:pending_friends应该是:friend,not:user。以下更正。

has_many :pending_friends, 
:through => :friendships, 
:conditions => "status = 'pending'", 
:foreign_key => "user_id", 
:source => :friend

1 个答案:

答案 0 :(得分:1)

我建议您为这些关联添加单元测试,因为它们看起来很复杂且不易理解。

  1. 添加灯具:

    # test/fixtures/users.yml
    current_user: 
      id: 1
      name: Current User
    another_user:
      id: 2 
      name: Your friend
    
    # test/fixtures/friendships.yml
    pending_friend:
      id: 1
      status: pending
      user_id: 1 # 
      # other attributes
    requested_friend:
      id: 2
      status: requested
      user_id: 2 # 
      # other attributes
    
  2. 编写单元测试代码,告诉自己关联是正确的:

    # test/model/user_test.rb
    class UserTest <  # extend Rails unit test class
      def test_the_requested_friendship 
        assert users(:current_user).requested_friends.include?( users(:another_user))
      end
      def test_the_pending_friendships
        # your assertion here
      end
    end
    
  3. 注意:上面的代码只是一个想法,我还没有对它们进行测试。你应该自己实现你的代码。

    我建议您使用“rspec”,因为它比传统的测试单元更具表现力。

相关问题