title: rails how to reference current object in Model
body: <p>This is a continuation of another question, but as it's different, I though I had better repost it as a new question:</p>
  
  <p><em>Old Question</em></p>
  
  <hr>
  
  <p>I'm adding quiz functionality to the twitter app from the Hartl tutorial and have these Models:</p>
  
  <p>User is nearly the same as the tutorial:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    has_many :followed_users, through: :relationships, source: :followed
  
    has_many :takens, dependent: :destroy
    has_many :questions, through: :takens
  end
  </code></pre>
  
  <p>Taken is a table of Question ids to User ids:</p>
  
  <pre><code>class Taken &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :question
  
  end
  </code></pre>
  
  <p>nothing interesting in Question:</p>
  
  <pre><code>class Question &lt; ActiveRecord::Base
    attr_accessible :category, :correct, :option1, :option2, :option3, :qn
  end
  </code></pre>
  
  <p>I want to be able to show followed_users and followers in order of the number of tests they have taken. In the console this can be had through:</p>
  
  <pre><code>User.find_by_id(1).question_ids.count
  </code></pre>
  
  <p>Then I can do something like:</p>
  
  <pre><code>User.find_by_id(1).followers.first.question_ids.count
  </code></pre>
  
  <p>in the console to get the count for a single follower.</p>
  
  <p>I feel like I'm almost there. </p>
  
  <p>How do I sort the followers and followed_users through their 'takens' count? (I was also looking at cache_count, which at first seemed promising, but might not be what I need...)</p>
  
  <hr>
  
  <p><em>End Old Question</em></p>
  
  <p>This is the answer from the other question: <a href="https://stackoverflow.com/questions/15870564/rails-order-through-count-on-other-table/15870644#comment22592709_15870644">rails order through count on other table</a></p>
  
  <p>and I went with a method like this in User.rb:</p>
  
  <pre><code>def users_sort_by_taken
  User.find_by_sql("SELECT users.*
                    SELECT users.*
                    FROM users INNER JOIN takens
                    ON users.id = takens.user_id
                    GROUP BY users.id
                    ORDER BY count(takens.user_id) DESC")
  end
  </code></pre>
  
  <p>which gets called in the users_controller.rb like so:</p>
  
  <pre><code>def following
      require 'will_paginate/array'
      @title = "Following"
      @user = User.find(params[:id])
      #@users = @user.followed_users.paginate(page: params[:page])
      @users = @user.users_sort_by_taken.paginate(page: params[:page])
      render 'show_follow'
  end
  </code></pre>
  
  <p>(For reference, the commented out line is from the Hartl tutorial)
  all well and good, but now the current user is contained in the list of following (because of the above SQL). I need a way to eliminate the current user from the users_sort_by_taken.</p>
  
  <p>I thought this might work:</p>
  
  <pre><code>WHERE (#{@current_user.id})
  </code></pre>
  
  <p>in the method,but I get this error:</p>
  
  <p>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id</p>
  
  <p>I suppose I could pass it as an argument...
  but don't I already have the user as @user in the following line?</p>
  
  <pre><code>@users = @user.users_sort_by_taken.paginate(page: params[:page])
  </code></pre>
  
  <p>Why can't I reference the current user from a method in the User.rb model?</p>
  
  <p>Or another way, can I pass the current_user (or @user or user) to the SQL to exclude the current_user from the SQL results?</p>
  
  <p>Any help appreciated.</p>