添加counter_cache时迁移出错

时间:2016-05-01 15:19:33

标签: ruby-on-rails database-migration

我在adwerts模型中为User列添加了counter_cache。

当我跑步时:

rake db:drop db:create db:migrate db:seed migration 

我收到以下错误:

rake aborted! NoMethodError: undefined method `limit' for nil:NilClass

我的迁移文件:

class AddAdwertsCounterCashInUser < ActiveRecord::Migration

  def self.up
    add_column :users, :adverts_count, :integer, default: 0, null: false

    User.reset_column_information
    User.all.each do |p|
      User.update p.id, adverts_count: p.adverts.length
    end
  end

  def self.down
    remove_column :users, :adverts_count
  end

end

怎么了?

0 个答案:

没有答案