Access ApplicationHelper from User.rb Model

Here is some simple code that doesn't work:

module ApplicationHelper def industries industries = ['Agriculture','Food', etc.] end end class User < ActiveRecord::Base include ApplicationHelper validates_inclusion_of :industries, :in => ApplicationHelper.industries ... end 

This code does not work when a user action is executed using the undefined method 'industries' . Do you know how to properly contact an assistant (or, preferably, to industries)?


Edit

According to the code below in Mauricio, how can INDUSTRIES access the controller and view? I have real problems with the fact that the industry is available everywhere, but it is really necessary for my application.

+3
source share
1 answer

You should never do something like that. ApplicationHelper is not intended to be included in the model. The best solution would be:

 class User < ActiveRecord::Base INDUSTRIES = ['Agriculture','Food'] validates_inclusion_of :industries, :in => INDUSTRIES end module ApplicationHelper def industries User::INDUSTRIES end end 

And now you have done it in a simple and enjoyable way.

EDIT

Anywhere you need to access industries you should use:

 User::INDUSTRIES 

What is it.

+7
source

All Articles