3

Here's some simple code that isn't working:

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 is failing when a user action is run with undefined method 'industries'. Do you know how I can reference the helper (or preferably just industries) the right way?


Edit

Based on the code in Mauricio's answer below, how can INDUSTRIES be accessed in a controller and a view? I'm having real trouble making industries accessible everywhere, but it really needs to be for my application.

Community
  • 1
  • 1
sscirrus
  • 55,407
  • 41
  • 135
  • 228

1 Answers1

7

You should never do something like this. ApplicationHelper is not supposed to be included in a model. 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 it done in a simple and nice way.

EDIT

Anywhere you need to access industries you should use:

User::INDUSTRIES

And that's it.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • 1
    Thanks for your answer Mauricio - could you tell me why I should never do what I was trying to do? – sscirrus Dec 17 '11 at 21:58
  • Also, how can I now access industries from my view? – sscirrus Dec 17 '11 at 22:25
  • Mauricio's answer sounds like the way to go. It might help you in future to know that you can also put globals in `config/application.rb` or any of the environment files. It's common to see people doing this when the info is not model specific - http://stackoverflow.com/questions/2562249/how-can-i-set-paperclips-storage-mechanism-based-on-the-current-rails-environme – stephenmurdoch Dec 18 '11 at 15:59
  • I have an analogous situation. When I follow Mauricio's answer, Rspec crashes: C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.8.0/lib/rspec/ core/configuration.rb:698: stack level too deep (SystemStackError) – George Shaw May 06 '12 at 18:52
  • I should note that one of the spec/support files includes ApplicationHelper. – George Shaw May 06 '12 at 19:43
  • There's probably something different in your spec that's causing this "stack level too deep". This usually means that there is a method eternally calling itself. – Maurício Linhares May 06 '12 at 22:09
  • It happens only when I apply your solution. When my equivalent of industries (states and countries) are defined in ApplicationHelper.rb and I include ApplicationHelper just before the model class in the model file then Rspec runs fine. When I move the code to the model file and remove the includes as you detail, Rspec crashes. – George Shaw May 07 '12 at 18:35
  • I decided to start over and try it again, and now it works. Must have gotten something wrong. Is adding `industries` to ApplicationHelper there to make it available in the views? – George Shaw May 07 '12 at 19:03
  • @GeorgeShaw it will, all views. – Maurício Linhares May 07 '12 at 19:15