2
# application_helper.rb

def do_some_stuff
   ...
end


# my_model.rb

def my_model_method
   # I want to call the method "do_some_stuff" here, how exactly?
end

Obviously, I can't just call do_some_stuff, since it would tell me that the model does not have this method.

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
TomDogg
  • 3,803
  • 5
  • 33
  • 60

1 Answers1

3

You can accomplish this by adding the following line to your model file:

include ActionView::Helpers

Now, you may want to reconsider placing your helper method somewhere else (e.g. the model, or a mixin module), but use the above line to do what you've asked for.

Rebitzele
  • 3,252
  • 1
  • 20
  • 21
  • You shouldn't give such an answer to this question. Of course it is possible in Ruby/Rails (as most things one can imagine are) but it's a very very very bad thing to do. – Mike Szyndel Jun 18 '13 at 18:20
  • @Michał Szyndel - Do you mean Joe Half Face's comment is the correct answer? Or do you suggest something else? – TomDogg Jun 18 '13 at 18:53
  • @TomDogg of course you can do that. But it's a very bad thing to do. If there's a piece of code that you need in you model - it should be in model. Then you may call in in controller and in view (on instance or class) – Mike Szyndel Jun 19 '13 at 12:41