ruby-on-rails – PostgreSQL – group by子句或用于聚合函数(错
发布时间:2020-12-17 01:30:15 所属栏目:百科 来源:网络整理
导读:我正在关注这个导演视频: http://railscasts.com/episodes/382-tagging. 在Postgres上使用我的Rails 3.2应用程序进行标记. 除标签云功能外,一切都很完美.出于某种原因,云功能在使用Heroku的Production中不起作用. 我收到这个错误 ActionView::Template::Err
我正在关注这个导演视频:
http://railscasts.com/episodes/382-tagging. 在Postgres上使用我的Rails 3.2应用程序进行标记. 除标签云功能外,一切都很完美.出于某种原因,云功能在使用Heroku的Production中不起作用. 我收到这个错误 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新手….请帮忙:) VIEWS ##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 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 end create_table "users",:force => true do |t| t.string "name" t.string "email" t.datetime "created_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 解决方法
这解决了我的问题:)
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |