在Ruby on Rails中,计数器缓存出错时,如何排查原因?

更新于
2026-10-09 21:56:51
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计308个文字,预计阅读时间需要2分钟。

在Ruby on Rails中,计数器缓存出错时,如何排查原因?

我在使用Rails 3.2.6时尝试进行计数器缓存,遇到了某种错误。我该如何解决这个问题?我在其他应用中做过类似操作,但在这个模型上没有成功。我的代码或相关部分可能缺少了什么?

我正在使用Rails 3.2.6.当我尝试进行计数器缓存时,我会以某种方式得到此错误.
我怎样才能解决这个问题?我在这个应用程序上做了同样的事情,但没有在这个模型上.
我的代码或关联有什么问题?

命令包exec rake db:migrate

日志

== AddCommunityTopicsCountToCommunity: migrating ============================= -- add_column(:communities, :community_topics_count, :integer, {:default=>0}) -> 0.0635s rake aborted! An error has occurred, all later migrations canceled: community_topics_count is marked as readonly

车型/ community.rb

... has_many :community_topics ...

车型/ community_topic.rb

在Ruby on Rails中,计数器缓存出错时,如何排查原因?

... belongs_to :community, counter_cache: true ...

迁移文件

class AddCommunityTopicsCountToCommunity < ActiveRecord::Migration def up add_column :communities, :community_topics_count, :integer, :default => 0 Community.reset_column_information Community.all.each do |p| p.update_attribute :community_topics_count, p.community_topics.length end end def down remove_column :communities, :community_topics_count end end

class AddCommunityTopicsCountToCommunity < ActiveRecord::Migration def up add_column :communities, :community_topics_count, :integer, :default => 0 Community.reset_column_information Community.all.each do |c| Community.reset_counters(c.id, :community_topics) end end end

本文共计308个文字,预计阅读时间需要2分钟。

在Ruby on Rails中,计数器缓存出错时,如何排查原因?

我在使用Rails 3.2.6时尝试进行计数器缓存,遇到了某种错误。我该如何解决这个问题?我在其他应用中做过类似操作,但在这个模型上没有成功。我的代码或相关部分可能缺少了什么?

我正在使用Rails 3.2.6.当我尝试进行计数器缓存时,我会以某种方式得到此错误.
我怎样才能解决这个问题?我在这个应用程序上做了同样的事情,但没有在这个模型上.
我的代码或关联有什么问题?

命令包exec rake db:migrate

日志

== AddCommunityTopicsCountToCommunity: migrating ============================= -- add_column(:communities, :community_topics_count, :integer, {:default=>0}) -> 0.0635s rake aborted! An error has occurred, all later migrations canceled: community_topics_count is marked as readonly

车型/ community.rb

... has_many :community_topics ...

车型/ community_topic.rb

在Ruby on Rails中,计数器缓存出错时,如何排查原因?

... belongs_to :community, counter_cache: true ...

迁移文件

class AddCommunityTopicsCountToCommunity < ActiveRecord::Migration def up add_column :communities, :community_topics_count, :integer, :default => 0 Community.reset_column_information Community.all.each do |p| p.update_attribute :community_topics_count, p.community_topics.length end end def down remove_column :communities, :community_topics_count end end

class AddCommunityTopicsCountToCommunity < ActiveRecord::Migration def up add_column :communities, :community_topics_count, :integer, :default => 0 Community.reset_column_information Community.all.each do |c| Community.reset_counters(c.id, :community_topics) end end end