Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 friendship model : how to ignore a friend request?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI think you're over-complicating things. I'm pretty sure that to do what you want to do, all you'd need would be a friend and friendship model and in the friendship model you'd have an int variable called status or something that would have different values depending on whether the relationship had been accepted or not. But that's beside the point. `<%= link_to "Ignore", friendship_path(Friendship_id), :method => :delete %>` should work, could you please show the destroy action of your friendships controller?
      singulars
    2. CO@Ashitaka: thanks for the reply! I added the destroy action in the question. You are right, there are other ways to do it, but I feel I could learn much by being able to do it this way. The problem is that being inside the show page of the user_6, even if the right friendship_id is passed, it fails because it says "cannot find Friendship_id=18 WHERE user_id =6 ... I'd like to somehow get rid of this user_id=6 ... I spent hours trying tons of options, I feel a bit confused now :-/ ... thanks anyway !
      singulars
    3. COOk, to solve this, we need a little bit more info. Please show your relationship model. And also, how do you distinguish between a user who is your friend and a user who sent you a friend request?
      singulars
 

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