4
votes

I have a model Sport. Using single table inheritance I have 2 other models, Cricket and Football so that

class Cricket < Sport and class Football < Sport

I put these two models in a subfolder inside models directory called sports. I added the type column to Sport and put the value as Cricket or Football, whatever was appropriate. Also, I was able to create objects using Cricket.new or Football.new.

This structure works fine till Rails 3.2.6.

But now with Rails 3.2.11, any model file inside a subfolder has to be modularised. So, it looks like this now:

module Sports
  class Cricket < Sport

Now, rails is not able to load the class Cricket or Football alone. So, Cricket.new or Football.new does not work. If I do Sports::Cricket.new, then it is a problem for me because the type column has to be the class name, i.e. Sports::Cricket.

What should I do in Rails 3.2.11 for single table inheritance to work? I don't want to put values like Sports::Cricket in my type column. Also, I do not want to remove the subdirectory structure.

2

2 Answers

1
votes

I just dealt with a similar problem myself. See How to reload files in app/models/subdirectory in dev environment for STI. Basically you need to require_dependency your subclasses on initalization and reload, in the parent class or in config files somewhere. It's a rails lazy load thing, and it doesn't seem like they want to change it. I found Alex Reisner's post incredibly helpful too.

0
votes

Have you tried to use this?

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]