Use a Rails helper in a model [duplicate]
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 generati开发者_高级运维ng 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?
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
class Post < ActiveRecord::Base
  include ActionView::Helpers::DateHelper
  def my_test_method(a, include_seconds = false)
    time_ago_in_words(a, include_second)
  end
 end
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论