我正在关注这个导演视频: http://railscasts.com/episodes/382-tagging。 在Postgres上使用我的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
我知道错误来自在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
SCHEMA
ActiveRecord::Schema.define(:version => 20130728204128) do
create_table "dailyposts", :force => true do |t|
t.string "title"
t.text "content"
t.text "content_html"
t.integer "user_id"
t.integer "to_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
end
add_index "dailyposts", ["user_id", "created_at"], :name => "index_dailyposts_on_user_id_and_created_at"
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "dailypost_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "taggings", ["dailypost_id"], :name => "index_taggings_on_dailypost_id"
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
t.string "password_reset_token"
t.datetime "password_reset_sent_at"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
add_index "users", ["username"], :name => "index_users_on_username", :unique => true
end
答案 0 :(得分:7)
这解决了我的问题:)
Dailypost模型
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
<强> AplicationHelper 强>
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