尝试使用 gsub 更改属性时出现迁移问题

发布于 2024-10-22 23:40:52 字数 1173 浏览 1 评论 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

I have a table called items. I want to change the description attribute (it's a text attribute) for 9 of the objects. Specifically, these objects are a subclass to Item -- called Juice. So the items table is STI. Here's a sample description of the item:

Boost all payouts by 20%.

Now, when I try to run the following migration, I can't get the dang description to update. Any ideas? (Rails version is 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-10-29 23:40:52

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

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

I have a hunch that j.description returns a copy of the string, not the actual structure that maps to the database; The gsub! call changes the wrong object.

Try j.description = j.description.gsub(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文