如何运行使用 strip_tags 的数据修改迁移?
我正在向现有表添加一个新列 summary
。它将包含 body
列中 HTML 的纯文本片段。
我想在运行迁移时为所有现有电子邮件创建摘要。但是,我不知道如何在迁移中使用 strip_tags。
到目前为止,这是我所拥有的:
class AddSummaryToEmails < ActiveRecord::Migration
self.up
add_column :emails, :summary, :string, :limit => 100
Email.reset_column_information
Emails.all.each do |email|
email.update_attributes(:summary => strip_tags(email.body))
end
end
...
end
当然,这是行不通的: #
如何在迁移中访问 strip_tags 方法?我知道我可以运行正则表达式或其他此类解决方法,但我仍然热衷于找出如何执行此操作以供将来使用。
谢谢
I'm adding a new column, summary
, to an existing table. It will contain a plaintext snippet of the HTML from the column body
.
I want to create summaries for all existing emails when I run my migration. However, I can't figure out how to use strip_tags within my migration.
Here's what I have so far:
class AddSummaryToEmails < ActiveRecord::Migration
self.up
add_column :emails, :summary, :string, :limit => 100
Email.reset_column_information
Emails.all.each do |email|
email.update_attributes(:summary => strip_tags(email.body))
end
end
...
end
Of course, this doesn't work:undefined method 'strip_tags' for #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0xb6e29be8>
How can I access the strip_tags method within my migration? I know I can run a regexp or another such workaround but am still keen to find out how to do this for future usage.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
strip_tags
是一个 ActionView 方法,并且您的迁移继承自 ActiveRecord,因此它看不到 ActionView 方法。不过,您可以通过这种方式获取它们:
如果您尝试包含 ActionView 变体,您将得到
未定义的方法“full_sanitizer”
,因为您需要扩展类方法,等等。更多的是痛苦。Since
strip_tags
is an ActionView method and your migration inherits from ActiveRecord, it can't see the ActionView methods.You can get to them this way, though:
If you try including the ActionView variant, you'll get
undefined method 'full_sanitizer'
because you need to extend the class methods, and so on. Much more of a pain.因为 strip_tags 超出了范围。您无权访问 ActionView::Helpers::SanitizeHelper。
请参阅相关问题
Because strip_tags is out of scope. You don't have access to ActionView::Helpers::SanitizeHelper.
See related question
wesgarrison 的解决方案不适用于 Rails 2.3.5 版本,因此为了使其工作,我必须直接从迁移中的 HTML::FullSanitizer 模块调用 HTML sanitize 方法,例如this:
希望这可以帮助那些和我有同样问题的人。
wesgarrison's solution didn't work for me with Rails 2.3.5 version, so in order to make it work I had to directly call HTML sanitize method from the HTML::FullSanitizer module in the migration like this:
Hope this helps someone who has the same problem as me.