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?
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?
                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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With