我正在尝试将counter_cache添加到我的标记表
当我运行迁移以添加它中止的列时(但不是在添加列之前) 我不明白它,虽然它成为专栏,它将停止任何未来的迁移。我该如何解决这个问题?
== AddCountToTags: migrating =================================================
-- add_column(:tags, :reports_count, :integer, {:default=>0})
-> 0.1405s
rake aborted!
An error has occurred, all later migrations canceled:
wrong number of arguments (0 for 1)
追踪:
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/counter_cache.rb:17:in `reset_counters'
/Dropbox/Shared/repair/db/migrate/20120120234938_add_count_to_tags.rb:5:in `up'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:314:in `block in migrate'
/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/benchmark.rb:295:in `measure'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:314:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:397:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:539:in `block (2 levels) in migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:615:in `call'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:615:in `ddl_transaction'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:538:in `block in migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:525:in `each'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:525:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:435:in `up'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/migration.rb:417:in `migrate'
/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.10/lib/active_record/railties/databases.rake:151:in `block (2 levels) in <top (required)>'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
迁移文件:
class AddCountToTags < ActiveRecord::Migration
def self.up
add_column :tags, :reports_count, :integer, :default => 0
Tag.reset_counters
Tag.find(:all).each do |t|
t.update_attribute(:reports_count, t.reports.length)
end
end
def self.down
remove_column :tags, :reports_count
end
end
相关模型:
class TagAssignment < ActiveRecord::Base
belongs_to :tag, :counter_cache => :reports_count
belongs_to :report
end
class Tag < ActiveRecord::Base
has_many :tag_assignments, :dependent => :destroy
has_many :reports, :through => :tag_assignments
end
答案 0 :(得分:2)
从我看到here,您需要将ID传递给reset_counters
。
似乎您想要做的事情应该是(未经测试)完成:
Tag.find_each do |t|
Tag.reset_counters(t.id, :reports)
end