Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 friendship model : how to ignore a friend request?
    text
    copied!<p>I have the traditional friendship model: </p> <p>The user model has:</p> <pre><code>has_many :friendships, :dependent =&gt; :destroy has_many :friends, :through =&gt; :friendships, :dependent =&gt; :destroy has_many :inverse_friendships, :class_name =&gt; "Friendship", :foreign_key =&gt; "friend_id", :dependent =&gt; :destroy has_many :inverse_friends, :through =&gt; :inverse_friendships, :source =&gt; :user, :dependent =&gt; :destroy </code></pre> <p>I would like to define the status "friend" only if a friend request has been sent, and accepted by the other user. Everything works fine except that I did not manage to process the "ignore friend request" part.</p> <p>What I am trying to do is: on a user profile page, there's a list of requests from other user, waiting approval. The user can then accept a friend request (then it becomes a friend) or reject it (then the friendship relationship is destroyed).</p> <p>Here is the piece of code, in the friendship controller, when it blocks:</p> <pre><code>&lt;h2 class="small_v_space"&gt;Waiting your approval&lt;/h2&gt; &lt;ul&gt; &lt;% for user in @user.inverse_friends %&gt; &lt;% if user.friends.exists?(@user) and not @user.friends.exists?(user) %&gt; &lt;li&gt; &lt;%=h user.name %&gt; (&lt;%= link_to "Accept", friendships_path(:friend_id =&gt; user), :method =&gt; :post %&gt;, &lt;%= link_to "Ignore", friendship_path, :controller =&gt; :friendships, :method =&gt; :delete %&gt;) &lt;/li &lt;% end %&gt; &lt;% end %&gt; &lt;/ul&gt; </code></pre> <p>The problem is that if I do like this, the delete method will delete the last added relationship, instead of the one linked to the ignore button. </p> <p><strong>Let's work with an example</strong>: <em>Here is the relationship I would like to destroy:</em> User_id: 10 Friend_id: 6 Friendship_id: 18</p> <p>I'm on the 'show' page (profile) of user, whose user_id is 6. I see that user 10 has made a friend request that I would like to ignore. Even if I managed to extract the proper Friendship_id, doing:</p> <pre><code>&lt;%= link_to "Ignore", friendship_path(Friendship_id), :controller =&gt; :friendships, :method =&gt; :delete %&gt;) </code></pre> <p>It results in "Cannot find Friendships with ID=18[where user_id=6]"</p> <p>Does anyone know how I could call the destroy action on the right relationship in this case ? Or should I proceed differently ?</p> <p>Many thanks for any clue!</p> <p><strong>EDIT</strong></p> <p><em>Destroy action of the friendship controller:</em></p> <pre><code>def destroy @friendship = current_user.friendships.find(params[:id]) if @friendship.friend.friends.exists?(current_user) @friendship.destroy flash[:notice] = "Removed friendship." else @friendship.destroy flash[:notice] = "Removed friend request." end redirect_to current_user end </code></pre> <p><strong>EDIT 2:</strong></p> <pre><code>class Friendship belongs_to :user belongs_to: :friend, :class_name =&gt; 'User' end </code></pre> <p>User A sends a friend request to user B (A=user, B=friend in the instance of the class). If B accepts the requests, then another instance is created (B=user, A=friend). If there exists both a relation A->B and B->A, A is a friend of B. Otherwise, the request remains pending (or can be ignored, rejected...).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload