Rails迁移无法识别新列

时间:2013-09-11 22:26:03

标签: ruby-on-rails rails-migrations

我正在尝试迁移迁移以转移一些数据。在上一次迁移中,我运行以下命令:

add_column :portfolios, :owner_id, :integer

然后在以下迁移中如下:     class MoveUserIdToPortfolio< ActiveRecord的::迁移

  class Stake < ActiveRecord::Base
  end

  class UserStakeInPortfolio < Stake
    belongs_to :portfolio, foreign_key: :possession_id

    def self.sti_name
      'UserStakeInPortfolio'
    end
  end

  class User < ActiveRecord::Base
    has_many :user_stake_in_portfolios, foreign_key: :owner_id
    has_many :portfolios, through: :user_stake_in_portfolios
    has_many :personal_portfolios, through: :user_stake_in_portfolios, :class_name => 'PersonalPortfolio', :source => :portfolio, :conditions => { :type => 'PersonalPortfolio' }

    def personal_portfolio
      self.personal_portfolios.first
    end
  end

  class Portfolio < ActiveRecord::Base
  end

  class PersonalPortfolio < Portfolio

    def self.sti_name
      'PersonalPortfolio'
    end
  end

  def up

    puts "DEBUG: #{Portfolio.inspect}"

    Portfolio.connection.schema_cache.clear!
    Portfolio.reset_column_information
    PersonalPortfolio.connection.schema_cache.clear!
    PersonalPortfolio.reset_column_information
    User.connection.schema_cache.clear!
    User.reset_column_information

    puts "DEBUG2: #{Portfolio.inspect}"

    User.all.each do |user|
      pp = user.personal_portfolio
      puts "DEBUG3: #{pp.inspect}"
      pp.owner_id = user.id
      pp.save
      puts "DEBUG4: #{pp.inspect}"
    end

  end

  def down

  end
end

这里奇怪的是,pp.owner_id永远不会被设置。并且输出(后面的内容)从未显示它尝试更新owner_id,尽管它没有错误输出。

(0.1ms)  BEGIN
(0.3ms)  UPDATE `portfolios` SET `updated_at` = '2013-09-11 22:16:06' WHERE `portfolios`.`type` IN ('PersonalPortfolio') AND `portfolios`.`id` = 4
(0.4ms)  COMMIT

DEBUG语句显示Portfolio没有owner_id属性。但DEBUG2输出一个。但是两个pp.inspect输出都没有显示owner_id的实例。

如果它有用,请尽快提供更多信息!

0 个答案:

没有答案