I'm trying to implement a trending feature on my app. I have a table called Search
that has a keyword
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:
Search.where('created_at >= ?', 4.hours.ago).group(:keyword).order('count_keyword DESC').limit(6).count(:keyword)
I tried turning the same query into a named scope and here's what I got:
scope :trending, lambda { where('created_at >= ?', 4.hours.ago).group(:keyword).order('count_keyword DESC').limit(6).count(:keyword) }
The problem here is that I get this error:
ArgumentError: Unknown key: thesearchkeyword
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?