Cameron
Cameron

Reputation: 28803

Followers model in Rails

In my app Users can follow each other. I have two tables: users and followers.

users

id
name

followers

id
user_id
follower_id

And two models:

class User < ActiveRecord::Base

  has_many :followers
  belongs_to :follower

end

class Follower < ActiveRecord::Base

  belongs_to :user

end

What I want to do is list the followers for a user. e.g.

<ul>
  <% @users.each do |user| %>
      <li><%= user.name %></li>
      <ul>
        <% user.followers.each do |follower| %>
            <li><%= follower.name %></li>
        <% end %>
      </ul>
  <% end %>
</ul>

However it doesn't look to be seeing the association...


After updating my model as per Deep's suggestions I got it working... however I'm unable to query followers and following users.

e.g.

I have two methods in my UsersController:

def user_followers
  @user = User.where(id: params[:id]).first
  @followers = @user.get_followers
end

def user_following
  @user = User.where(id: params[:id]).first
  @following = @user.get_following
end

and then in my User model I have:

def get_followers
  Follower.where(follower_id: self.id)
end

def get_following
  Follower.where(user_id: self.id)
end

Which should be returning the followers for the user or who the user is following. The views look like so:

<% @followers.each do |follower| %>
    <%= follower.user.name %>
<% end %>

<% @following.each do |following| %>
    <%= following.user.name %>
<% end %>

However it only returns the name of the user I'm supposed to be viewing.

The Follower model now looks like:

class Follower < ActiveRecord::Base

  belongs_to :user, foreign_key: 'follower_id', class_name: 'User'

end

Upvotes: 0

Views: 1177

Answers (1)

Deepesh
Deepesh

Reputation: 6398

The follower.name will not work because your follower model has no attribute named name. Also the association you have given is belongs_to :user which will fetch the user who has been followed. You need another association in the Follower model like:

belongs_to :following_user, foreign_key: 'follower_id', class_name: 'User'

And then in your view what you can do is:

<%= follower.following_user.name %>

This will fetch the user who has followed and from that object it will fetch the name attribute.

Update:

Don't remove the existing association you have. Means your model should look like:

belongs_to :user    
belongs_to :following_user, foreign_key: 'follower_id', class_name: 'User'

Now as discussed in comments user_id will consist of the user who is being followed and follower_id will consist of the user who is following.

To fetch the followers of a user you have the association which will find the followers. Just like user.followers. And to fetch the name:

<% user.followers.each do |follower| %>
   <li><%= follower.following_user.name %></li>
<% end %>

To find the followed users:

def followed_users
  Follower.where(follower_id: id)
end

And to fetch the name:

<% user.followed_users.each do |follower| %>
   <li><%= follower.user.name %></li>
<% end %>

Upvotes: 1

Related Questions