Search code examples
ruby-on-railshas-many-throughtable-relationshipsself-reference

I am trying to change follower status from 'requested' to 'approved' in relationship model


I have a relationship table with followed_id, follower_id, and status. I am using "has many through" in my user model to create the association. A user has many following through and is following many through a reverse relationship. The status defaults to "requested" when a user follows another user.

So then a user has a list of their followers and each has a status of "requested".

What I need in this list of followers is a link_to with a put method or a form that just finds that relationship and updates the status string to "approved". It seems like it would be pretty easy but I have been searching for days and cannot figure it out. This is what I have so far:

In the users_controller this the action to change the status from "requested" to "approved":

def activate
  @user = User.find(params[:id])
  params[:status] = {:status => 'approved'}
  @user.status = params[:user][:status]
  @user.save
  render 'show_followers'
end 

this is in the users_controller to list the followers:

def followers
 @title = "Followers"
 @user = User.find(params[:id])
 @users = @user.followers.paginate(:page => params[:page])
 render 'show_followers'
end

this is where I define status which is probably not right in user.rb model

def status
    Relationship.status.find_by_follower_id(self)
end

and this is the page where I show the followers:

      <% unless @users.empty? %>
        <% @users.each do |user| %>
        <tr>
            <td><%= image_tag user.avatar.url(:thumb) %></td>
            <td><%= user.full_name %></td>
            <td><%= user.company %></td>
            <td><%= user.approval.status %></td>
        <%end%>
            <td><%= form_for @user, url: activate_user_path(@user), :html => { :method => :put } do |f| %>
                <%= f.submit "approve" %>
                <% end %>
            </td>

        </tr>
        <%= will_paginate @users %>
        <% end %>

does anyone know how I can just update that string in Relationship table to "approved"?


Solution

  • I see a potential problem in your form, think it should be for user and not @user & params[:user][:status] in activate method (params[:user][:status] should be nil as it has not been defined in your method). I'd rather have a remote link alongside all followers that are to be approved, then update the status using the link, but that bit is for later.

    So, the form :

    <%= form_for user, url: activate_user_path(user), :html => { :method => :put } do |f| %>
      <%= f.submit "approve" %>
    <% end %>
    

    and, assuming you've access to current user, and if not, passing current user too, i.e. @user via the form.

    def activate
      user = User.find(params[:id]) # user to be approved
      relationship = current_user.relationships.where(:follower_id => user).first # relationship record that has above user as follower and current user as followed
      relationship.update_attributes!(:status => 'approved')
      # redirect to users#followers passing current_user
    end