使用回数に基づいてタグを選択し、その順序で配置する次のタグメソッドがあります。コードは railscasts のエピソード ( http://railscasts.com/episodes/382-tagging ) に基づいています。
以前は動作していた mysql から、スタック トレースで生成されたエラー メッセージを確認できる postgres に DB を変更しました。
postgresql で動作するようにこの sql をリファクタリングするにはどうすればよいですか?
def tags
Tag.joins(:taggings).select('tags.*, count(tag_id) as "tag_count"').group(:tag_id).order('tag_count desc')
end
スタックトレース
ActiveRecord::StatementInvalid - PG::GroupingError: ERROR: column "tags.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT tags.*, count(tag_id) as "tag_count" FROM "tags" INNE...
^
: SELECT tags.*, count(tag_id) as "tag_count" FROM "tags" INNER JOIN "taggings" ON "taggings"."tag_id" = "tags"."id" GROUP BY tag_id ORDER BY tag_count desc:
タグ.rb
class Tag < ActiveRecord::Base
has_many :taggings
has_many :questions, through: :taggings
#omitted for brevity
tagging.rb
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :question
end
さらにコードが必要な場合は、ただ叫んでください。