mysql - The Ruby way to group and count, refactor -


i have table stores users input search field. wrote action displays popular searches , number of times search has been made. wrote following sql query in controller...

select `query`,        count(`query`) `query_count`  `searches` [foo things] group `query` order `query_count` desc 

... , pass variable search.find_by_sql(query), save instance variable, , iterate on array each block , printing out search['query_count']. want, i'd love know how in ruby way, because know it's bad practice have sql queries in controller. know how put where conditions named_scopes, don't know how replicate rest of query in ruby.

i'm using mysql, ruby 1.8.7, rails 2.3.15.

should easy as:

search.select(:query, 'count(query) query_count').where(foo things).group(:query).order('query_count desc') 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -