Use a Rails helper in a model

Joe Flynn picture Joe Flynn · Jul 25, 2011 · Viewed 10.8k times · Source

Possible Duplicate:
Access a view helper for a model in rails

I know this is probably not something I should do often, but I want to use a helper within a model.

I'm generating a spreadsheet in a Report model and would like to use the time_ago_in_words method in ActionView::Helpers::DateHelper

Is this doable, or should I be generating it in a view?

Answer

Joe Flynn picture Joe Flynn · Jul 25, 2011

You can just include the module in the model, and use it like you would in a view

class Report
  include ActionView::Helpers::DateHelper
  ...
end