Forum: Ruby on Rails Helpers in Models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E0065184aad5afbd13f0c3c57ff9180c?d=identicon&s=25 Rudi W. (whitespace)
on 2009-03-13 14:34
I have an app that relies on a series of helpers to generate language
specific text strings for all output.

In one of my models I have to save one of these language specific
strings to the table.
Therefore I've started investigating how I can accesss my helpers in my
model - I know this is not best practise, but its the only way I can see
to make this work.

I know of the solution with including the helper in the model like this:

 class Employee < ActiveRecord::Base
   include LanguageHelper
 end

But this is not that good performance wise?

How do I reach my helpers in the model in a more efficient way?
Or can I construct my code in a more correct way?

Best regards
Rudi
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-03-13 14:56
Rudi W. wrote:
>Therefore I've started investigating how I can accesss my helpers in my
>model - I know this is not best practise, but its the only way I can see
>to make this work.

So your idea here is to fix a bad practice by using an even worse
practice?

> How do I reach my helpers in the model in a more efficient way?
> Or can I construct my code in a more correct way?

Dealing with languages is a view specific tasks. Models should not be
concerned with language specific issues, In my view the pain this is
giving you should be viewed like a "toothache." Your code is trying to
tell you something is fundamentally wrong. Using pain relievers, might
get you by for a time, but eventually the problem will need to be
addresses at the source.

However, if you have code that needs to be shared between the MVC layers
it's common practice to put that code in modules inside the lib
directory, which is provided for just this sort of sharing. That doesn't
mean, however, that lib is intended to be a "free zone" where rules of
MVC don't matter and you're free to break it at will.
This topic is locked and can not be replied to.