5

I am building an app to generate a character for the Star Wars RPG. I added the Species model. Species is the same singular as it is plural, which is throwing me for a loop. I can get the routes to work fine in the app by using species_index_path, but for some reason, the has_many :characteristics, :through => :species_characteristics isn't working in one direction.

For instance, I have two models that are properly seeded (and work) in the console: Characteristics and Species. Characteristics is setup in the following way:

class Characteristic < ActiveRecord::Base
    has_many :species_characteristics
    has_many :species, :through => :species_characteristics
    has_many :skills
end

Species is setup in the following way:

class Species < ActiveRecord::Base
    has_many :species_characteristics
    has_many :characteristics, :through => :species_characteristics
end

The model between them just has a belongs_to for each of them.

If I call Characteristic.first.species from the console, I get the list of species related to that characteristic.

If I call Species.first.characteristics, however, I get the following:

NameError: uninitialized constant Species::Characteristics

I looking in to adding a new inflection, or some way around this but I'm coming up with nothing. Does anyone have a better method short of renaming the model to something like Race?


TLDR: has_many relationship doesn't work only in one direction due to an uninitialized constant error, likely because of an inflection issue. Is there any way to fix it other than renaming the model?
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    Maybe related to [Rails: Handling a scaffolding, such as "Sheep," that has the same plural and singular form](http://stackoverflow.com/questions/7489054/rails-handling-a-scaffolding-such-as-sheep-that-has-the-same-plural-and-sin) – sawa Aug 01 '13 at 12:57
  • 1
    it seems a bug to me; consider to report to [Rails issues](https://github.com/rails/rails/issues), if it isn't already there – mdesantis Aug 01 '13 at 12:57
  • 1
    And this: http://underpop.free.fr/r/ruby-on-rails/cookbook/I_0596527314_CHP_2_SECT_6.html – sawa Aug 01 '13 at 12:59
  • 1
    @sawa, that's what I thought.. but then I tried "characteristics".singularize.pluralize and it worked. – ilan berci Aug 01 '13 at 13:16
  • @sawa I've tried adding `inflect.plural 'species', 'species'` and `inflect.uncountable %w( species )` to the inflections file and neither seem to work. So even though it's related to the issue, I don't think it's quite the same. I appreciate you linking that one for me to check out though. – Seriouslysean Aug 01 '13 at 14:08

1 Answers1

3

After hours of tinkering with it, I figured out the issue. I removed any extra changes I made to the inflections.rb file so it looked like this:

ActiveSupport::Inflector.inflections(:en) do |inflect|
    inflect.uncountable %w( species )
end

Then I went through and double checked all of the models to make sure the syntax was correct, the relationships made sense and things of that nature. Turns out, I was using has_many :species, :through => :species_characteristics when it should have been has_many :species, through: :species_characteristics. Once I fixed those issues in the Species and the Characteristics models and did reload! in the console, everything started working.


Characteristic Model:

class Characteristic < ActiveRecord::Base
    has_many :species_characteristics
    has_many :species, through: :species_characteristics
end

Species Model:

class Species < ActiveRecord::Base
    has_many :species_characteristics
    has_many :characteristics, through: :species_characteristics
end

SpeciesCharacteristic Model:

class SpeciesCharacteristic < ActiveRecord::Base
    belongs_to :characteristic
    belongs_to :species
end

Thus ends this issue's reign of terror.