13

I'm trying to add a counter_cache and this Rails 3 migration is giving me an error I just can't seem to resolve.

The migration is

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

and the error is:

== AddItemsCountToStore: migrating ========================================= -- add_column(:stores, :items_count, :integer, {:default=>0}) -> 0.0680s rake aborted! An error has occurred, this and all later migrations canceled:

items_count is marked as readonly C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.0/lib/active_record/persistence.rb:115:in `update_attribute'

Any ideas?

Adam Mikulasev
  • 615
  • 1
  • 5
  • 12

0 Answers0