Search code examples
rubyruby-on-rails-5single-table-inheritance

Why are subclasses not initialized in Rails Single Table Inheritance?


I'm trying to make a simple "book signing" app on Rails (books, authors, bookstores, etc.) using single table inheritance (class Bookstore < Company).

I have the following in config/application.rb:

config.autoload_paths += %W(#{config.root}.app/models/company)

Both company.rb and bookstore.rb are in the app/models/company directory.

Bookstore.create(name: "Barnes and Noble") is in seeds.rb

When I run rails db:seed I get an error uninitialized constant Bookstore

Also if I'm in the rails console and I do Bookstore.new(...), I get the same error, but if I do Company.new(...) I get undefined method 'new' for Company:Module which was surprising because I thought Company was a class:

company.rb has class Company < ApplicationRecord

How do I set up single table inheritance so that I can store classes in sub directories?

I'm using rails 5.2

Thanks!


Solution

  • Turns out there's a typo:

    config.autoload_paths += %W(#{config.root}.app/models/company)

    should be

    config.autoload_paths += %W(#{config.root}/app/models/company)

    => slash in front of the app directory instead of a dot