4

I am using Ruby on Rails to create a website for a game I play.

I have a User model and a Starbase model. The relationship I am trying to setup is like so

class User < ActiveRecord::Base
  has_many :starbases
end

class Starbase < ActiveRecord::Base
  belongs_to :user
end

However when I open script/console and try to access the users starbases it gives me an error: NameError: uninitialized constant User::Starbasis.

It seems as if it is a problem with inflection and rails is not pluralizing starbase correct.

I have tried adding this to the inflections.rb in the intializers folder:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'starbase', 'starbases'
end

but it still does not solve the issue. Could anyone give advice on how to get this working?

1
  • Not sure, but doesn't this change require a server restart?
    – Warren
    Jan 13, 2011 at 18:59

2 Answers 2

5

Have you tried adding a line for the inverse inflection (i.e. 'singular'):

 inflect.singular "starbases", "starbase"

I tried your example in my console and it was the singularization that caused problems, not the other way around. I'm not sure if this fixes other issues (like routes), but it should fix the simple stuff (I think).

4
  • Hey, I have tried this but unfortunately I am still getting the same error.
    – RailsSon
    Jan 13, 2011 at 21:40
  • 1
    Hey, I got it working with inflect.irregular "starbase", "starbases". Marking your question as correct. Cheers for the response.
    – RailsSon
    Jan 13, 2011 at 21:55
  • That was my next suggestion, but it worked on my setup as I proposed so didn't want to confuse the issue. Nice work...
    – Nuby
    Jan 14, 2011 at 1:51
  • 1
    Great! Was just banging my head against the wall with another model name (Tax -> Taxes -> Taxis). Using inflect.singular didn't work, but inflect.irregular did.
    – nfm
    Apr 21, 2011 at 3:42
0

Little trick i picked up to double check how Active Support might singularize, or pluralize my Class names, and/or Module names.

have your rails app server running and in a new tab enter into your rails console by typing rails console. In there you can easily double check for the correct style for your names.

long way ActiveSupport::Inflector.pluralize "fish" # => "fish"

short way "fish".pluralize # => "fish"

You can find more examples here

https://github.com/rails/rails/blob/master/activesupport/test/inflector_test_cases.rb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.