Hello,
I would like to rewrite this find_by_sql query in terms of :group, :limit, etc. options. I don't know how to handle the count(*) as count part though. Does anybody know how to make a more Ruby-like implementation of the following function?
class Tag < ActiveRecord::Base def self.find_top_tags(options = {}) options[:order] ||= 'count desc, name asc' options[:limit] ||= 100 query = "select tags.id, name, count(*) as count" query << " from taggings, tags" query << " where tags.id = tag_id" query << " and context = '#{options[:on]}'" if options[:on] != nil query << " group by tag_id" query << " order by #{options[:order]}" if options[:order] != nil query << " limit #{options[:limit]}" if options[:limit] != nil tags = Tag.find_by_sql(query) end end
P.S. I am using acts_as_taggable_on, and finding that this function would be nice to have.