Use Rails Helper in Model

Possible duplicate:
Access to view assistant for model in rails

I know that this is probably not something I should do often, but I want to use an assistant within the model.

I am creating a spreadsheet in the Report model and would like to use the time_ago_in_words method in ActionView::Helpers::DateHelper

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

+6
source share
2 answers

You can simply include the module in the model and use it in the same way as in the view

 class Report include ActionView::Helpers::DateHelper ... end 
+15
source
 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 
0
source

Source: https://habr.com/ru/post/893515/


All Articles