Permalink
Browse files

Postgresql require selected columns to apear in group by statement

and ordered columns to apear in select statement when using distinct,
so we have to use inner queries.
  • Loading branch information...
1 parent 49a40d9 commit af0147e62f7950f1c8b05f8acd29fe2d1d62f68b Kamil Giszczak committed with tomeric May 19, 2010
Showing with 12 additions and 2 deletions.
  1. +12 −2 lib/acts_as_taggable_on/acts_as_taggable_on/core.rb
@@ -173,8 +173,18 @@ def all_tags_list_on(context)
##
# Returns all tags of a given context
def all_tags_on(context)
- opts = ["#{ActsAsTaggableOn::Tagging.table_name}.context = ?", context.to_s]
- base_tags.where(opts).order("#{ActsAsTaggableOn::Tagging.table_name}.created_at").group("#{ActsAsTaggableOn::Tagging.table_name}.tag_id").all
+ tag_table_name = ActsAsTaggableOn::Tag.table_name
+ tagging_table_name = ActsAsTaggableOn::Tagging.table_name
+
+ taggable_type = self.class.name
+ ActsAsTaggableOn::Tag.find_by_sql([ "SELECT DISTINCT tags.* FROM (
+ SELECT #{tag_table_name}.* FROM #{tag_table_name}
+ INNER JOIN #{tagging_table_name} ON #{tag_table_name}.id = #{tagging_table_name}.tag_id
+ WHERE ((#{tagging_table_name}.taggable_id = ?) AND (#{tagging_table_name}.taggable_type = '#{taggable_type}'))
+ AND (#{tagging_table_name}.context = ?)
+ GROUP BY #{tag_table_name}.id, #{tag_table_name}.name, #{tagging_table_name}.tag_id, #{tagging_table_name}.created_at
+ ORDER BY #{tagging_table_name}.created_at
+ ) as tags", id, context.to_s ])
end
##

0 comments on commit af0147e

Please sign in to comment.