This migration worked fine on an initial developer machine but is failing in a second dev environment. Setups are identical as far as we can tell.
Migration:
class AddInventoryItemsCountToProduct < ActiveRecord::Migration
def change
add_column :products, :inventory_items_count, :integer, default: 0
reversible do |dir|
dir.up do
Product.all.each do |p|
p.update_attribute :inventory_items_count, p.inventory_items.length
end
end
end
end
end
Haven't seen this error. Anyone know what's going on?
$ rake db:migrate
== AddInventoryItemsCountToProduct: migrating ================================
-- add_column(:products, :inventory_items_count, :integer, {:default=>0})
-> 0.0112s
rake aborted!
An error has occurred, this and all later migrations canceled:
inventory_items_count is marked as readonly/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/persistence.rb:506:in `verify_readonly_attribute'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/persistence.rb:217:in `update_attribute'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:8:in `block (3 levels) in change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:7:in `block (2 levels) in change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:474:in `up'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:6:in `block in change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:510:in `block in reversible'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:687:in `execute_block'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:510:in `reversible'
/Users/meltemi/rails/input/myapp/db/migrate/20130823022914_add_inventory_items_count_to_product.rb:5:in `change'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:571:in `exec_migration'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in `block in migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in `ddl_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in `block in migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in `each'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in `up'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in `migrate'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/bin/ruby_noexec_wrapper:14:in `eval'
/Users/meltemi/.rvm/gems/ruby-2.0.0-p247@myapp/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Check this answer: Problem with counter_cache implementation Also you might to take the 2nd advice: find_each instead of each