Ruby on rails PostgreSQL-GROUPBY子句或在聚合函数中使用(错误)
我正在关注rails的这段视频: . 在Postgres上使用my Rails 3.2应用程序进行标记。 除了标签云功能外,一切都很完美。由于某些原因,云功能在使用Heroku的生产环境中不起作用 我收到这个错误Ruby on rails PostgreSQL-GROUPBY子句或在聚合函数中使用(错误),ruby-on-rails,ruby,postgresql,heroku,tagging,Ruby On Rails,Ruby,Postgresql,Heroku,Tagging,我正在关注rails的这段视频: . 在Postgres上使用my Rails 3.2应用程序进行标记。 除了标签云功能外,一切都很完美。由于某些原因,云功能在使用Heroku的生产环境中不起作用 我收到这个错误 ActionView::Template::Error (PGError: ERROR: column "tags.id" must appear in the GROUP BY clause or be used in an aggregate function def ta
ActionView::Template::Error (PGError: ERROR: column "tags.id" must appear
in the GROUP BY clause or be used in an aggregate function
def tag_cloud(tags, classes)
max = tags.sort_by(&:count).last
tags.each do |tag|
index = tag.count.to_f / Integer(max.count) * (classes.size - 1)
yield(tag, classes[index.round])
end
end
我知道错误来自于在Heroku中使用Postgres,但我不知道如何修复它。我已经在下面发布了我的代码
Rails新手…请帮助:)
视图
##The error occurs here..
<div id="tag_cloud">
<% tag_cloud Dailypost.tag_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
class Tag < ActiveRecord::Base
attr_accessible :name
has_many :taggings
has_many :dailyposts, through: :taggings
end
class Tagging < ActiveRecord::Base
attr_accessible :dailypost_id, :tag_id
belongs_to :tag
belongs_to :dailypost
end
class Dailypost < ActiveRecord::Base
attr_accessible :title, :content, :content_html, :tag_list
belongs_to :user
has_many :taggings
has_many :tags, through: :taggings
def self.tagged_with(name)
Tag.find_by_name!(name).dailyposts
end
##I know i have to change the method below, but I do not understand how...Please Help
def self.tag_counts
Tag.select("tags.*, count(taggings.tag_id) as count").
joins(:taggings).group("taggings.tag_id")
end
def tag_list
tags.map(&:name).join(", ")
end
def tag_list=(names)
self.tags = names.split(",").map do |n|
Tag.where(name: n.strip).first_or_create!
end
end
end
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_many :dailyposts, dependent: :destroy
end
这解决了我的问题:)
每日邮报模式
def self.tag_counts
Tag.select("tags.id, tags.name,count(taggings.tag_id) as count").
joins(:taggings).group("taggings.tag_id, tags.id, tags.name")
end
应用程序助手
ActionView::Template::Error (PGError: ERROR: column "tags.id" must appear
in the GROUP BY clause or be used in an aggregate function
def tag_cloud(tags, classes)
max = tags.sort_by(&:count).last
tags.each do |tag|
index = tag.count.to_f / Integer(max.count) * (classes.size - 1)
yield(tag, classes[index.round])
end
end