从头开始标记:标记云问题 [英] Tagging from Scratch: the Tag Cloud Issue

查看:125
本文介绍了从头开始标记:标记云问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 http://railscasts.com/episodes/382-tagging 的说明进行操作a>并从头开始构建标签系统.一切都很好,但不是tag_cloud助手.搜索tag_counts时会引发数据库错误.

I'm following the instructions from http://railscasts.com/episodes/382-tagging and building a Tag System from Scratch. Everything are working great but not the tag_cloud helper. It raises database error while searching for tag_counts.

以下范围:

#Picture.rb

class Picture < ActiveRecord::Base

  attr_accessible :description, :title, :tag_list

  has_many :taggings
  has_many :tags, through: :taggings

#Because of the following I'm getting an error from the Posgresql (showed in "Database Error")

  def self.tag_counts 

    Tag.select("tags.*, count(taggings.tag_id) as count").
      joins(:taggings).group("taggings.tag_id")
  end
end

Application_helper.rb

def tag_cloud(tags, classes)
  max = tags.sort_by(&:count).last
  tags.each do |tag|
    index = tag.count.to_f / max.count * (classes.size - 1)
    yield(tag, classes[index.round])
end

数据库错误

ActiveRecord :: Statement在Pictures#index中无效

Database Error

ActiveRecord::StatementInvalid in Pictures#index

PG::Error: column "tags.id" ​​should appear in the GROUP BY clause or be used in an aggregate function

LINE 1: SELECT tags.*, count(taggings.tag_id) as count FROM "tags" I...

               ^
: SELECT tags.*, count(taggings.tag_id) as count FROM "tags" INNER JOIN "taggings" ON "taggings"."tag_id" = "tags"."id" GROUP BY taggings.tag_id

推荐答案

您应按tags.id分组,和/或对taggings.id进行计数

You should group by tags.id, and/or count taggings.id

Tag.select("tags.*, count(taggings.id) as count").
  joins(:taggings).group("tags.id")

您不能在查询中同时分组和聚合.

you can't group and aggregate at the same time in a query.

这篇关于从头开始标记:标记云问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆