Sean M.
Sean M.

Reputation: 593

Uninitialized constant in view

I have two models:

Post:

class Post < ActiveRecord::Base 
    has_many :exes
end

Exe:

class Exe < ActiveRecord::Base
    belongs_to :post
end

What I am getting in my view on http://localhost:3000/posts/index is:

NameError in Posts#index
uninitialized constant Post::Ex

It says just Ex for some reason.

The code of line ruby is complaining on is <% post.exes.each do |exe| %> which looks right to me.

So I don't really know why this is happening. If have also checked the following as i thought this might be the reason but no:

2.0.0-p247 :004 > ActiveSupport::Inflector.pluralize('Exe')
 => "Exes" 
2.0.0-p247 :005 > ActiveSupport::Inflector.singularize('Exe')
 => "Exe" 

Thanks in advance!

Upvotes: 0

Views: 199

Answers (2)

grandinero
grandinero

Reputation: 1225

Your problem is that ActiveSupport::Inflector is assuming that a word that ends in 'xes' in the plural form must end in 'x' in the singular form. See here for help on customizing pluralizations.

Update: Somehow I missed the last part of your question. You said you tried:

> ActiveSupport::Inflector.singularize('Exe')

but did you try:

> ActiveSupport::Inflector.singularize('Exes')

Upvotes: 2

zeantsoi
zeantsoi

Reputation: 26193

Define the inflector for this particular string in your project's inflections initializer:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'Exe', 'Exes'
end

Remember that you'll need to restart your server before changes will take effect.

Upvotes: 1

Related Questions