title: Followers model in Rails
body: <p>In my app Users can follow each other. I have two tables: <code>users</code> and <code>followers</code>.</p>
  
  <pre><code>users
  
  id
  name
  
  followers
  
  id
  user_id
  follower_id
  </code></pre>
  
  <p>And two models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    has_many :followers
    belongs_to :follower
  
  end
  
  class Follower &lt; ActiveRecord::Base
  
    belongs_to :user
  
  end
  </code></pre>
  
  <p>What I want to do is list the followers for a user. e.g.</p>
  
  <pre><code>&lt;ul&gt;
    &lt;% @users.each do |user| %&gt;
        &lt;li&gt;&lt;%= user.name %&gt;&lt;/li&gt;
        &lt;ul&gt;
          &lt;% user.followers.each do |follower| %&gt;
              &lt;li&gt;&lt;%= follower.name %&gt;&lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
    &lt;% end %&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>However it doesn't look to be seeing the association...</p>
  
  <hr>
  
  <p>After updating my model as per Deep's suggestions I got it working... however I'm unable to query followers and following users.</p>
  
  <p>e.g.</p>
  
  <p>I have two methods in my UsersController:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>and then in my User model I have:</p>
  
  <pre><code>def get_followers
    Follower.where(follower_id: self.id)
  end
  
  def get_following
    Follower.where(user_id: self.id)
  end
  </code></pre>
  
  <p>Which should be returning the followers for the user or who the user is following. The views look like so:</p>
  
  <pre><code>&lt;% @followers.each do |follower| %&gt;
      &lt;%= follower.user.name %&gt;
  &lt;% end %&gt;
  
  &lt;% @following.each do |following| %&gt;
      &lt;%= following.user.name %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>However it only returns the name of the user I'm supposed to be viewing.</p>
  
  <p>The Follower model now looks like:</p>
  
  <pre><code>class Follower &lt; ActiveRecord::Base
  
    belongs_to :user, foreign_key: 'follower_id', class_name: 'User'
  
  end
  </code></pre>