0

我正在尝试通过迁移来转移一些数据。在之前的迁移中,我运行以下命令:

add_column :portfolios, :owner_id, :integer

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

  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语句显示 aPortfolio没有owner_id属性。但DEBUG2输出它与一个。但是这两个pp.inspect输出都没有显示带有 的实例owner_id

如果有帮助,很高兴提供更多信息!

4

0 回答 0