Rails 迁移无法更新文本字段
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑
打印
— — ' “ ” king
gsub!
末尾的爆炸声表明它在适当的位置运行。请注意#gsub!
与#gsub
不会就地修改。您可以直接在
vs.text
上执行此操作,因为它已经是一个字符串,并完全跳过整个cleansed_text
操作。综上所述,我能想到的您的示例不起作用的唯一两个原因是
Poem
类没有方法text
通过ActiveRecord
表示poems
表中的text
列。虽然我知道您的更改当前尚未持久化,但通常认为在可能的情况下在
down
方法中使迁移可逆是一个好主意。如果您或您团队中的任何人使用 Windows 文本编辑器,请确保您的迁移 其中没有字节顺序标记,以防万一。
既然您提到了 1.8,请务必阅读 1.9 中的情况发生了怎样的变化。
由于这看起来像编码转换,如果您知道旧编码为什么不 使用
String#encode
?Consider
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 wholecleansed_text
thing entirely.All that said, the only two reasons I can think of that your example isn't working are
Poem
class doesn't have a methodtext
that represents a columntext
in yourpoems
table viaActiveRecord
.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
?