我正在尝试添加counter_cache,并且此Rails 3迁移给了我一个我似乎无法解决的错误。使用update_attribute运行Rails 3迁移时发生只读错误
迁移是
class AddItemsCountToStore < ActiveRecord::Migration def self.up add_column :stores, :items_count, :integer, :default => 0 Store.reset_column_information Store.all.each do |store| store.update_attribute :items_count, store.items.count end end def self.down remove_column :stores, :items_count end end
和错误是:
== AddItemsCountToStore:迁移====================== =================== - add_column(:stores,:items_count,:integer,{:default => 0}) - > 0.0680s rake中止! 发生错误,这和所有后来迁移取消:
items_count被标记为只读 C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/ persistence.rb:115:`update_attribute'
任何想法?
[计数器\ _cache实现问题]的可能重复(http://stackoverflow.com/questions/4295404/problem-with-counter-cache-implementation) – mbillard 2014-04-30 11:03:16