0

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!

Sean M.
  • 595
  • 1
  • 4
  • 21

2 Answers2

2

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')
Community
  • 1
  • 1
grandinero
  • 1,155
  • 11
  • 18
  • 1
    Thanks for your comment. For some reason I missed out on checking `singularize('Exes')`. That 'xes' must end in 'x' just didnt come to my mind because I am german and not quite familiar with english :) - Works fine now! – Sean M. Sep 12 '13 at 00:27
  • That's the standard form for pluralization in English. E.g. "box" => "boxes", "tax" => "taxes". A Rails lesson and an English lesson for the price of one! :) – grandinero Sep 12 '13 at 00:30
1

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.

zeantsoi
  • 25,857
  • 7
  • 69
  • 61