3

So I'm seeing this strange error when I try to do a fairly simple interactive test of an associations I've added. Here are the two models:

class Lot < ActiveRecord::Base
  has_many :graves
  belongs_to :block
end

class Grave < ActiveRecord::Base
  belongs_to :lot
end

Here are the table creation migrations:

class CreateGraves < ActiveRecord::Migration
  def change
    create_table :graves do |t|
      t.integer :grave_number
      t.integer :lot_id

      t.timestamps null: false
    end
  end
end

class CreateLots < ActiveRecord::Migration
  def change
    create_table :lots do |t|
      t.integer :lot_number
      t.integer :map_type

      t.timestamps null: false
    end
  end
end

I'm invoking pry with:

pry -r ./config/environment.rb

Then in the pry session I simply do:

lot = Lot.new
l.graves

and I get this error:

NameError: uninitialized constant Lot::Grafe
from /.../activerecord-4.2.6/lib/active_record/inheritance.rb:158:in `compute_type'

The ... there is simply the path to my rbenv installation and the ruby 2.3.0 subdirectory chain. I replaced it in there to keep that output readable.

I've got several other similar associations defined on other classes and all of those work as expected.

jaydel
  • 14,389
  • 14
  • 62
  • 98
  • 1
    Changing the class name from `Grave` to `Gravesite` took care of this. I don't know what gem in the system was interfering with the class name Grave, but a huge, noisy WTF at whichever one it was. – jaydel Aug 07 '16 at 15:13

1 Answers1

3

This is an issue with Rails' inflectors. It happens at weird times and is a strange Rails quirk.

2.3.1 :004 > a = "Grave"
 => "Grave"
2.3.1 :005 > a.pluralize
 => "Graves"
2.3.1 :006 > a = "graves"
 => "graves"
2.3.1 :007 > a.singularize
 => "grafe"

You can override the default behavior in the often overlooked ./config/inflections.rb file :)

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'grave', 'graves'
end

after change

2.3.1 :001 > a = "grave"
 => "grave"
2.3.1 :002 > a.pluralize
 => "graves"
2.3.1 :003 > a = "graves"
 => "graves"
2.3.1 :004 > a.singularize
 => "grave"
engineerDave
  • 3,887
  • 26
  • 28
  • wow, I thought I'd just have to file this one under "I will never know" permanently. It won't let me award the bounty for 18 more hours, but you'll be getting it. Thanks so much. – jaydel Sep 08 '16 at 19:29
  • 1
    yeah the clue was in the error message "NameError: uninitialized constant Lot::Grafe" <- note the constant Grafe instead of Grave. :) – engineerDave Sep 08 '16 at 19:33
  • So I found a random website showing the various ways to conjugate the Old English verb 'grafan' which means `to dig, dig up; to grave; engrave, chisel, carve`. One of the conjugations is indeed "grafe". – jaydel Sep 08 '16 at 19:36
  • yeah not saying its wrong just that you can override it with what you'd expect/want it to be... English at times is a weird language as its such an amalgam of so many languages probably due to who occupied England at the time lol – engineerDave Sep 15 '16 at 16:43
  • yeah, it was more of an interesting factoid--made me wonder where they came up with the 'rules' for pluralization/singularization. I'd vote for saying it's wrong anyway :) – jaydel Sep 21 '16 at 13:19