加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如果我尝试进行计数器缓存,为什么会出现此错误

发布时间:2020-12-17 01:26:25 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 3.2.6.当我尝试进行计数器缓存时,我会以某种方式得到此错误. 我怎样才能解决这个问题?我在这个应用程序上做了同样的事情,但没有在这个模型上. 我的代码或关联有什么问题? 命令包exec rake db:migrate 日志 == AddCommunityTopicsCountToC
我正在使用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

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

迁移文件

class AddCommunityTopicsCountToCommunity < ActiveRecord::Migration
  def up
    add_column :communities,: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,:default => 0

    Community.reset_column_information
    Community.all.each do |c|
      Community.reset_counters(c.id,:community_topics)
    end
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读