title: Calling method to provide search parameters in Rails model
body: <p>I've got a Rails controller with the following pieces:</p>
  
  <pre><code>def search
    ...
    @player_to_team_histories = @player_to_team_histories.order(sort_column + " " + sort_direction)
    ...
  </code></pre>
  
  <p><code>sort_column</code> and <code>sort_direction</code> are defined as (<code>@player_to_team_histories</code> is an <code>ActiveRecordAssociation</code>:</p>
  
  <pre><code>private
  
    def sort_column
      params[:sort] || "players.LastName"
    end
  
    def sort_direction
      params[:direction] || "asc"
    end
  </code></pre>
  
  <p>So the text returned from those two functions gets inserted into the <code>order</code> clause on the <code>@player_to_team_histories</code>.</p>
  
  <p>I need to duplicate this search functionality in another controller, so I've moved the search logic to the <code>PlayerToTeamHistories</code> model and pass the <code>params</code> hash as a parameter to the <code>search</code> function.  Skeleton code:</p>
  
  <pre><code>def self.search_results(params)
   ...
    @player_to_team_histories = @player_to_team_histories.order(sort_column(params) + " " + sort_direction(params))
   ...
  </code></pre>
  
  <p>I tried defining <code>sort_column(params)</code> and <code>sort_direction(params)</code> in a module and kept getting an <code>undefined method or variable sort_columns</code> error, so I moved the implementation to the bottom of the model</p>
  
  <pre><code>def sort_column(params)
    params[:sort] || "players.LastName"
  end
  
  def sort_direction(params)
    params[:direction] || "asc"
  end
  </code></pre>
  
  <p>And still get the undefined method error.  I tried simplifying it to:</p>
  
  <pre><code>def sort_column
    "players.LastName"
  end
  
  def sort_direction
    "asc"
  end
  </code></pre>
  
  <p>But that still didn't work (I updated the call to the functions to be without <code>params</code>).</p>
  
  <p>Any idea why I can't call a helper function from within another function in a model?</p>
  
  <p>Rails 3.2.1, Ruby 1.9.2</p>