1

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

3 Answers 3

5

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.

1
  • Ah, thanks! I did try including ActionView::Helpers::SanitizeHelper and got stuck. * Goes back to Ruby OO 101 :) *
    – nfm
    Commented Jan 1, 2010 at 23:04
1

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

1

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:

def self.up
  HTML::FullSanitizer.new.sanitize('<p>Test</p>')
end

Hope this helps someone who has the same problem as me.

1
  • Both worked for me with Rails 3.2.13 but this one seems to be the better one. Commented Aug 14, 2013 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.