gpt4 book ai didi

ruby-on-rails - 从头开始标记 : the Tag Cloud Issue

转载 作者:行者123 更新时间:2023-12-04 05:45:05 27 4
gpt4 key购买 nike

我正在按照 http://railscasts.com/episodes/382-tagging 中的说明进行操作从头开始构建标签系统。一切都很好,但不是 tag_cloud 助手。它在搜索 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::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

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

您不能在查询中同时进行分组和聚合。

关于ruby-on-rails - 从头开始标记 : the Tag Cloud Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12806757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com