2012-01-29 8 views
0

私は私のタグテーブルに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 

答えて

2

私はあなたがreset_countersにIDを渡す必要がhere見るものから。働い

Tag.find_each do |t| 
    Tag.reset_counters(t.id, :reports) 
end 
+0

であなたがする必要があります何をしたいのか(未テスト)行われているようです!私はreset_countersを見上げるとは思わなかった。ちょうど新しい目が必要だったと思う。 –

+0

うれしいこと:) –

関連する問題