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
Since
strip_tagsis 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.