title: How do I turn this ActiveRecord query into a named scope?
body: <p>I'm trying to implement a trending feature on my app. I have a table called <code>Search</code> that has a <code>keyword</code> column. Every time a user uses the search function, the keyword is stored in the table as a row. What I did is I get the searches done for the past 4 hours, group them by keyword, limit the search to 6 keywords, and arrange them based on the number of occurrence of the keyword. At the moment, I'm using this query on the controller:</p>
  
  <pre><code>Search.where('created_at &gt;= ?', 4.hours.ago).group(:keyword).order('count_keyword DESC').limit(6).count(:keyword)
  </code></pre>
  
  <p>I tried turning the same query into a named scope and here's what I got:</p>
  
  <pre><code>scope :trending, lambda { where('created_at &gt;= ?', 4.hours.ago).group(:keyword).order('count_keyword DESC').limit(6).count(:keyword) }
  </code></pre>
  
  <p>The problem here is that I get this error:</p>
  
  <blockquote>
    <p>ArgumentError: Unknown key: thesearchkeyword</p>
  </blockquote>
  
  <p>I don't really know a lot about named scopes and reading the API doesn't help a lot since they only provide very simple examples. Could anyone point me in the right direction?</p>