I'm having this class method inside my Post model for archives
class Post < ActiveRecord::Base
def self.archives
# fetch archives
end
end
How can I convert this sql query to ActiveRecord query in order to put it in the self.archives
method?
SELECT YEAR(`created_at`) AS `year`, MONTHNAME(`created_at`) AS `month`, COUNT(`id`) AS `total`
FROM `posts`
GROUP BY `year`, `month`
ORDER BY `year` DESC, MONTH(`published_at`) DESC