diff --git a/app/models/album.rb b/app/models/album.rb index 2b5f0b4..c81c1da 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -30,9 +30,9 @@ class Album < ActiveRecord::Base end def self.popular_tags(limit = nil) - query = "SELECT tags.id, tags.name, count(*) AS count FROM albums_tags, tags, albums WHERE tags.id = tag_id AND albums_tags.album_id = albums.id GROUP BY tags.id, tags.name ORDER BY tags.name ASC" + query = "SELECT tags.id, tags.name, count(*) AS count FROM albums_tags, tags, albums WHERE tags.id = tag_id AND albums_tags.album_id = albums.id GROUP BY tags.id, tags.name ORDER BY count DESC" query << " LIMIT #{limit}" unless limit.nil? - Tag.find_by_sql(query) + Tag.find_by_sql(query).sort { |a, b| a.name <=> b.name } end protected diff --git a/app/models/page.rb b/app/models/page.rb index 0d13249..d0652fa 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -35,9 +35,9 @@ class Page < ActiveRecord::Base end def self.popular_tags(limit = nil) - query = "SELECT tags.id, tags.name, count(*) AS count FROM pages_tags, tags, pages WHERE tags.id = tag_id AND pages_tags.page_id = pages.id GROUP BY tags.id, tags.name ORDER BY tags.name ASC" + query = "SELECT tags.id, tags.name, count(*) AS count FROM pages_tags, tags, pages WHERE tags.id = tag_id AND pages_tags.page_id = pages.id GROUP BY tags.id, tags.name ORDER BY count DESC" query << " LIMIT #{limit}" unless limit.nil? - Tag.find_by_sql(query) + Tag.find_by_sql(query).sort { |a, b| a.name <=> b.name } end def cache_name diff --git a/app/models/photo.rb b/app/models/photo.rb index 16342f9..eaa9155 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -37,9 +37,9 @@ class Photo < ActiveRecord::Base end def self.popular_tags(limit = nil) - query = "SELECT tags.id, tags.name, count(*) AS count FROM photo_tags, tags, photos WHERE tags.id = tag_id AND photo_tags.photo_id = photos.id GROUP BY tags.id, tags.name ORDER BY tags.name ASC" + query = "SELECT tags.id, tags.name, count(*) AS count FROM photo_tags, tags, photos WHERE tags.id = tag_id AND photo_tags.photo_id = photos.id GROUP BY tags.id, tags.name ORDER BY count DESC" query << " LIMIT #{limit}" unless limit.nil? - Tag.find_by_sql(query) + Tag.find_by_sql(query).sort { |a, b| a.name <=> b.name } end protected