0

我有一张名为 items 的表。我想更改 9 个对象的描述属性(它是一个文本属性)。具体来说,这些对象是 Item 的子类——称为 Juice。所以项目表是 STI。这是该项目的示例描述:

将所有支出提高20%

现在,当我尝试运行以下迁移时,我无法更新该当的描述。有任何想法吗?(Rails 版本是 2.3.11。)

class ModifyItemJuiceDescription < ActiveRecord::Migration

  def self.up
    juices = Juice.all

    Juice.transaction do
      for j in juices do
        puts "Juice description is: #{j.description}."
        j.description.gsub!('payouts', 'winnings')
        puts "Juice description will be saved as: #{j.description}."
        j.save!
        puts "Juice description is now: #{j.description}."
        puts "======================================================"
      end
    end

  end

  def self.down
    juices = Juice.all

    Juice.transaction do
      for j in juices do
        puts "Juice description is: #{j.description}."
        j.description.gsub!('winnings', 'payouts')
        puts "Juice description will be saved as: #{j.description}."
        j.save!
        puts "Juice description is now: #{j.description}."
        puts "======================================================"
      end
    end

  end

end
4

1 回答 1

1

我有一种预感,它会j.description返回字符串的副本,而不是映射到数据库的实际结构;gsub!调用更改了错误的对象。

尝试j.description = j.description.gsub(...)

于 2011-03-19T14:58:35.470 回答