views:

83

answers:

2

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

+1  A: 

Because strip_tags is out of scope. You don't have access to ActionView::Helpers::SanitizeHelper. See related question

bobbywilson0
+1  A: 

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:

def self.up
  ActionController::Base.helpers.strip_tags("<b>TEST</b>")
end

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.

wesgarrison
Ah, thanks! I did try including `ActionView::Helpers::SanitizeHelper` and got stuck. * Goes back to Ruby OO 101 :) *
nfm