Rails 迁移无法更新文本字段

发布于 2024-12-17 01:38:22 字数 948 浏览 2 评论 0原文

我正在使用 Rails 3.1.1 和 Ruby 1.9.2。我正在将数据库从 Ruby 1.8.7 环境迁移到 Ruby 1.9 环境,并希望执行以下迁移:

# coding: utf-8
class CleanseVerses < ActiveRecord::Migration
  def up
    Poem.all.each do |vs|

      cleansed_text = String.new
      cleansed_text = vs.text

      cleansed_text.gsub!('—',  '—')
      cleansed_text.gsub!(' - ',  ' — ')
      cleansed_text.gsub!('’',  '’')
      cleansed_text.gsub!('“',  '“')
      cleansed_text.gsub!('â€', '”')
      cleansed_text.gsub!('prince', 'king')

      vs.text = cleansed_text

      vs.save

    end
  end

  def down
  end
end

问题是数据库似乎没有反映任何更改。即使是简单的“王子”到“国王”的转换也不起作用。

我在 Poem.text 字段上确实有 attr_accessible 。

我是否遗漏了一些关于弦乐的基本知识?

更新:

我似乎遗漏了一些关于字符串本质的基本知识。

当我更改以下两行时:

cleansed_text = String.new
cleansed_text = vs.text

一切都完美运行

cleansed_text = String.new(vs.text)

I am using Rails 3.1.1 and Ruby 1.9.2. I am moving a database from a Ruby 1.8.7 environment to a Ruby 1.9 environment and would like to execute the following migration:

# coding: utf-8
class CleanseVerses < ActiveRecord::Migration
  def up
    Poem.all.each do |vs|

      cleansed_text = String.new
      cleansed_text = vs.text

      cleansed_text.gsub!('—',  '—')
      cleansed_text.gsub!(' - ',  ' — ')
      cleansed_text.gsub!('’',  '’')
      cleansed_text.gsub!('“',  '“')
      cleansed_text.gsub!('â€', '”')
      cleansed_text.gsub!('prince', 'king')

      vs.text = cleansed_text

      vs.save

    end
  end

  def down
  end
end

The problem is that the database doesn't seem to reflect any of the changes. Even the simple 'prince' to 'king' conversion isn't working.

I do have attr_accessible on the Poem.text field.

Am I missing something fundamental on strings?

UPDATE:

It seems that I am missing something fundamental on the nature of strings.

When I change the following two lines:

cleansed_text = String.new
cleansed_text = vs.text

to

cleansed_text = String.new(vs.text)

then everything works perfectly.

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

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

发布评论

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

评论(1

长亭外,古道边 2024-12-24 01:38:22

考虑

# coding: utf-8
vs = '—  - ’ “ †prince'
vs.gsub!('—', '—')
  .gsub!(' - ',  ' — ')
  .gsub!('’',  '’')
  .gsub!('“',  '“')
  .gsub!('â€', '”')
  .gsub!('prince', 'king')
puts vs

打印 — — ' “ ” king

gsub! 末尾的爆炸声表明它在适当的位置运行。请注意 #gsub!#gsub 不会就地修改。

您可以直接在 vs.text 上执行此操作,因为它已经是一个字符串,并完全跳过整个 cleansed_text 操作。

综上所述,我能想到的您的示例不起作用的唯一两个原因是

  1. 您的更改没有被持久化,因为它们无法保存,并且
  2. 您的 Poem 类没有方法text 通过 ActiveRecord 表示 poems 表中的 text 列。

虽然我知道您的更改当前尚未持久化,但通常认为在可能的情况下在 down 方法中使迁移可逆是一个好主意。

如果您或您团队中的任何人使用 Windows 文本编辑器,请确保您的迁移 其中没有字节顺序标记,以防万一。


既然您提到了 1.8,请务必阅读 1.9 中的情况发生了怎样的变化

由于这看起来像编码转换,如果您知道旧编码为什么不 使用String#encode

Consider

# coding: utf-8
vs = '—  - ’ “ †prince'
vs.gsub!('—', '—')
  .gsub!(' - ',  ' — ')
  .gsub!('’',  '’')
  .gsub!('“',  '“')
  .gsub!('â€', '”')
  .gsub!('prince', 'king')
puts vs

which prints — — ’ “ ” king

The bang on the end of gsub! indicates that it operates in place. Note that #gsub! is not the same as #gsub which does not modify in place.

You can perform this operation directly on vs.text since it's already a string and skip the whole cleansed_text thing entirely.

All that said, the only two reasons I can think of that your example isn't working are

  1. Your changes are not being persisted because they are failing to save, and
  2. Your Poem class doesn't have a method text that represents a column text in your poems table via ActiveRecord.

While I understand that your changes are not currently being persisted, it is generally considered to be a good idea to make your migrations reversible in the down method when at all possible.

If you or anyone on your team uses a Windows text editor, make sure your migration doesn't have a byte order mark in it, just in case.


Since you mention 1.8, do read about how things have changed in 1.9.

And since this looks like an encoding conversion, if you know the old encoding why don't you use String#encode?

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